相当于Java中System.currentTimeMillis(),精确到毫秒
DateTime Epoch = new DateTime (1970, 1, 1);
Console.WriteLine((long)(DateTime.UtcNow - Epoch).TotalMilliseconds);
相当于Java中System.currentTimeMillis(),精确到毫秒
DateTime Epoch = new DateTime (1970, 1, 1);
Console.WriteLine((long)(DateTime.UtcNow - Epoch).TotalMilliseconds);
默认情况下,当目标网站出现问题时(404 Not Found,502 Bad Gataway等),curl还是会返回错误的内容,可以给curl设置一个参数,以避免这种情况。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
设置CURLOPT_FAILONERROR为1,当远程网页返回Http状态码大于400时,content就为空。
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] picture=baos.toByteArray();
很多组件在Windows Phone中都缩水,比如WebClient类,只有异步下载功能,没同步下载功能.貌似Windows Phone中大多数跟网络相关的功能都默认异步,这样有个好处,比如在显示一张网络图片,不需要像Android中一样新开线程下载图片了,直接把Source设置成图片URL即可.但是没有同步下载功能,很多时候会很麻烦,比如说我们下载后要对数据进行处理,但是如果异步下载的话,下载好的数据只有在WebClient下载完调用的方法中才能得到,这样就需要把参数传递给下载完成的函数,比较麻烦. 同步下载原理:其实还是异步下载,只是使用了AutoResetEvent,让线程在开始下载的时候等待,下载完成事件里,通知原来的线程继续执行.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
delegate void DownDelegate(string s);
//定义委托,非UI线程需要与UI线程通讯,必须通过委托
DownDelegate downDelegate;
string html;
public void CompletedDown(string s)
{
textBlock1.Text = s;
}
public MainPage()
{
InitializeComponent();
}
AutoResetEvent done = new AutoResetEvent(false);
//AutoResetEvent可以让线程等待,直到收到通知
private void button1_Click(object sender, RoutedEventArgs e)
{
downDelegate = CompletedDown;
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
}
public void ThreadProc()
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted+=new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri("http://www.pocketdigi.com"));
done.WaitOne();
//等待,收到通知后继续执行
this.Dispatcher.BeginInvoke(downDelegate, html);
}
private void webClient_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
{
html = e.Result;
done.Set();
//通知线程继续执行
}
}
}
很多对象如List, ObservableCollection,在删除对象时,都需要该类实现IEquatable接口中的Equals方法,以判断是否符合条件. Equals方法很简单,只需要判断两者所有的变量,属性值是否相同即可.下面是帮助文档中的例子:
public class Box : IEquatable
{
public Box(int h, int l, int w)
{
this.Height = h;
this.Length = l;
this.Width = w;
}
public int Height { get; set; }
public int Length { get; set; }
public int Width { get; set; }
public bool Equals(Box other)
{
if (this.Height == other.Height & this.Length == other.Length
& this.Width == other.Width)
{
return true;
}
else
{
return false;
}
}
}
当表中包含有NText,Text,Xml,Image的列时,使用DataContext.SubmitChanges() ,只要修改或者删除,都会报SQL Server does not handle comparison of NText, Text, Xml, or Image data types,这是因为Linq会比例所有的列.解决方法:给主键加上IsVersion=true属性.
我在我的ListBox的Item里加了一个按钮,点击按钮可以删除该项,费了很长时间,终于在一国外论坛上找到解决方法. 在Button的click事件中,Button的DataContext属性就是这个Item对象,简单地转换成绑定的ItemsSource的对象就可以.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
VideoItem item = button.DataContext as VideoItem;
System.Diagnostics.Debug.WriteLine("删除"+item.Title);
}
单行的TextBox,如果用户按回车键,说明输入已经完成,但Windows Phone并不会自动隐藏软键盘盘.单行的TextBox在按回车键后会触发TextInput事件,而TextBox在失去焦点后,软键盘就会隐藏,所以重写这个事件的方法,把焦点转移到其他控件上即可:
private void textBox1_TextInput(object sender, TextCompositionEventArgs e)
{
System.Diagnostics.Debug.WriteLine(textBox1.Text);
this.Focus();
//转移焦点到整个页面,TextBox失去焦点后,就会隐藏软键盘
}
在xaml代码中,可以用{StaticResource PhoneAccentBrush}这样的方式引用系统内置的资源,如果需要在代码中引用,可以用下面的方法:
stackPanel1.Background = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]);
在声明函数的时候加个unsafe关键字,如
public unsafe void parseVideo(int * total)
调用时,必须放进fixed语句内,内调用的方法也需要unsafe,如
fixed (int* p = &FirstMax) { videoXml.parseVideo(p); };