我有這個代碼執行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