Windows Phone 8.1中的后台任务上的显示对话框出现问题 [英] Problems with show dialog on Background task in windows phone 8.1
本文介绍了Windows Phone 8.1中的后台任务上的显示对话框出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码用于在后台方法中执行httpwebrequest和响应,我只想在下载zip崩溃并且我的代码进入此捕获时显示对话框以获取信息.
I have this code for execute httpwebrequest and response in background method and i just want show dialog for information when download zip crashed and my code enter in this catch...
private void DoSincroFit()
{
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.BeginGetResponse(new AsyncCallback(playResponseAsync), request);
}
public async void playResponseAsync(IAsyncResult asyncResult)
{
//Declaration of variables
HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
try
{
string fileName = "sincrofit.rar";
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult))
{
byte[] buffer = new byte[1024];
var newZipFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
var newZipFile = await KnownFolders.DocumentsLibrary.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (var writeStream = await newZipFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (var outputStream = writeStream.GetOutputStreamAt(0))
{
using (var dataWriter = new DataWriter(outputStream))
{
using (Stream input = webResponse.GetResponseStream())
{
var totalSize = 0;
for (int size = input.Read(buffer, 0, buffer.Length); size > 0; size = input.Read(buffer, 0, buffer.Length))
{
dataWriter.WriteBytes(buffer);
totalSize += size; //get the progress of download
}
await dataWriter.StoreAsync();
await outputStream.FlushAsync();
dataWriter.DetachStream();
}
}
}
}
}
}
catch
{
SMethods.Message_Dialog("Download has stopped!","Error");
}
}
但是当我的代码执行此方法时,请从此类中进行:
But when my code execute this method, from this class:
class StandarMethods
{
public async void Message_Dialog(string text, string title)
{
//Declaration of variables
MessageDialog MDialog = new MessageDialog(text, title);
await MDialog.ShowAsync();
}
}
最后,我的应用在尝试执行时崩溃:
Finally my app crash when try to execute:
await MDialog.ShowAsync();
这正在等待后台任务...有人可以帮助我吗?可能是时候使用事件处理程序了吗?为什么?如何?预先感谢!
This await in background task... Someone can helps me? It's possible time to use Event Handlers? Why? How? Thanks in advance!
推荐答案
解决了,我的最终代码在这里:
Solved, and my final code is here:
private CoreDispatcher dispatcher;
private void DoSincroFit()
{
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
//Add headers to request
request.Headers["Type"] = "sincrofit";
request.Headers["Device"] = "1";
request.Headers["Version"] = "0.000";
request.Headers["Os"] = "WindowsPhone";
dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
request.BeginGetResponse(new AsyncCallback(playResponseAsync), request);
}
public async void playResponseAsync(IAsyncResult asyncResult)
{
//Declaration of variables
HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
try
{
//For download file with stream
//http://social.msdn.microsoft.com/Forums/windowsapps/en-US/de96a61c-e089-4595-8349-612be5d23ee6/download-file-with-httpwebrequest?forum=winappswithcsharp
string fileName = "sincrofit.rar";
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult))
{
byte[] buffer = new byte[1024];
//For acces Local folder of phone device
//http://social.msdn.microsoft.com/Forums/windowsapps/en-US/ec99721c-6565-4ce9-b6cc-218f2265f9c7/what-is-the-uri-of-an-isolatedstorage-file?forum=wpdevelop
var newZipFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (var writeStream = await newZipFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (var outputStream = writeStream.GetOutputStreamAt(0))
{
using (var dataWriter = new DataWriter(outputStream))
{
using (Stream input = webResponse.GetResponseStream())
{
var totalSize = 0;
for (int size = input.Read(buffer, 0, buffer.Length); size > 0; size = input.Read(buffer, 0, buffer.Length))
{
dataWriter.WriteBytes(buffer);
totalSize += size; //get the progress of download
dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
//Declaration of variables
pBar.Value = sizeFit / totalSize * 100;
});
}
await dataWriter.StoreAsync();
await outputStream.FlushAsync();
dataWriter.DetachStream();
}
}
}
}
}
}
catch
{
dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
//Declaration of variables
SMethods.Message_Dialog("Download has stopped!", "Error");
});
}
}
感谢您的时间@loop!
Thanks for your time @loop!
这篇关于Windows Phone 8.1中的后台任务上的显示对话框出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文