2012-02-19 7 views
1

私はwp7のためにC#でアプリケーションを書いています:2ページ[mainpage、secondpage]。
アプリケーションはメインページで開始され、ユーザーは第2ページで(NavigationService.Navigateを使用して)secondpageにナビゲートできます。wp7のバックグラウンドでファイルをダウンロードする

secondpage WebClientでは、isolatedStorageにファイルをダウンロードします。

私の問題は、ユーザーが戻るキーを使用してメインページに戻るときにダウンロードがフリーズすることです。

バックグラウンドでこれを行う方法があります。そのため、ユーザーはページを自由にナビゲートできますか?

ここには、secondpageクラスのコードがあります(clickイベントにwebClient.OpenReadAsync(uri)のボタンもあります)。 BackgroundWorkerのクラスと

public partial class SecondPage : PhoneApplicationPage 
{ 
    WebClient webClient = new WebClient(); 
    IsolatedStorageFile Storage = IsolatedStorageFile.GetUserStoreForApplication(); 

    public SecondPage() 
    { 
     InitializeComponent(); 
     webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged); 
     webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); 
    } 
    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
    { 
     try 
     { 
      if (e.Result != null) 
      { 
       string fileName = "download.txt"; 
       IsolatedStorageFileStream f = new IsolatedStorageFileStream(fileName, System.IO.FileMode.Create, Storage); 
       long fileNameLength = (long)e.Result.Length; 
       byte[] byteImage = new byte[fileNameLength]; 
       e.Result.Read(byteImage, 0, byteImage.Length); 
       f.Write(byteImage, 0, byteImage.Length); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

    void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
    { 
     try 
     { 
      if (ProgressDownload.Value <= ProgressDownload.Maximum) 
      { 
       ProgressDownload.Value = (double)e.ProgressPercentage; 
      } 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
} 

おかげ

私はこの問題を持っている:その呼び出しは非同期であるので、私は、bw_doWork機能(コードは下にある)が終了webClient.OpenReadAsyncを呼び出すときに! bwはcompleteEventを報告します。

private void bw_DoWork(object sender, DoWorkEventArgs e) 
    { 
     BackgroundWorker worker = sender as BackgroundWorker; 

     WebClient webClient = new WebClient(); 
     webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged); 
     webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); 


     webClient.OpenReadAsync(new Uri("http://foo.com/asd.txt")); 

    } 
+1

BackgroundWorkerクラスをチェックしましたか?http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.95).aspx –

答えて

0

Background Transfer APIsを確認してください。これは、要件を満たすために必要なものです。

+0

これは私が必要とするものです。ありがとうございました! – Matteo

関連する問題