我有这个代码执行httpwebrequest和响应背景方法,我只想显示对话框的信息,当下载邮编崩溃,我的代码输入在此捕获...在Windows Phone 8.1中的背景任务显示对话框的问题
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");
}
}
但是,当我的代码执行这种方法,从这个类:
class StandarMethods
{
public async void Message_Dialog(string text, string title)
{
//Declaration of variables
MessageDialog MDialog = new MessageDialog(text, title);
await MDialog.ShowAsync();
}
}
最后我的应用程序崩溃时,尝试执行:
await MDialog.ShowAsync();
这是在后台任务等待...有人可以帮助我吗?有可能使用事件处理程序?为什么?怎么样?提前致谢!
完美!但是这段代码给了我一个小小的错误...错误:“对象引用没有设置和对象的实例” 这是一个密封的类,没有构造函数methods.Any idea?谢谢大家! – 2014-09-08 07:38:00
在哪一行? – loop 2014-09-08 09:23:02
我更新了ans检查它。 – loop 2014-09-08 09:34:23