超时取消任务:
private async void button_Click(object sender, RoutedEventArgs e)
{
var feedUri = new Uri("http://news.163.com/special/00011K6L/rss_newstop.xml");
var client = new Windows.Web.Syndication.SyndicationClient();
try
{
CancellationTokenSource cs = new CancellationTokenSource(10);
var feed = await client.RetrieveFeedAsync(feedUri).AsTask(cs.Token);
}
catch (Exception ex)
{
if (ex.GetType() == typeof(System.Threading.Tasks.TaskCanceledException))
{
await new MessageDialog("Message:Task Canceled!").ShowAsync();
}
else
{
await new MessageDialog("Message:Exception!").ShowAsync();
}
}
}
WinRT事件发生顺序:
Page_PointerEntered
Page_PointerPressed
Page_PointerReleased
Tapped //如果长按或按着移动就没有
Page_PointerExited
属性设置:ManipulationMode=”All”
Page_PointerPressed
Page_ManipulationStarting
Page_ManipulationStarted//只有触摸滑动时才触发
Page_ManipulationDelta
Page_ManipulationDelta
Page_ManipulationCompleted
Page_PointerReleased
Windows:
属性设置:CanDrag="True" AllowDrop="True"
Page_PointerPressed
DragStarting
DragEnter //拖动窗口
DragOver //移动
_Drop //放下
DropCompleted //完成
Page_PointerReleased
HttpClient的网络请求是支持进度监控,通过异步任务的IProgress对象可以直接监控到HttpClient的网络请求返回的进度信息,返回的进度对象是HttpProgress类对象。在进度对象HttpProgress里面包含了下面的一些信息:Stage(当前的状态)、BytesSent(已发送的数据大小)、BytesReceived(已接收的数据大小)、Retries(重试的次数)、TotalBytesToSend(总共需要发送的数据大小)和TotalBytesToReceive(总共需要接收的数据大小)。网络请求进度监控的代码示例如下所示:
// 创建IProgress<HttpProgress>对象
IProgress<HttpProgress> progress = new Progress<HttpProgress>(ProgressHandler);
// 在异步任务中加入进度监控
HttpResponseMessage response = await httpClient.PostAsync(new Uri(resourceAddress), streamContent).AsTask(cts.Token, progress);
// 进度监控的回调方法
private void ProgressHandler(HttpProgress progress)
{
// 在这里可以通过progress参数获取到进度的相关信息
}