2017-05-26 4 views
0
string url = "http://any_urls"; 
    MediaPlayer player = new MediaPlayer(); 

    public MainPage() 
    { 
     InitializeComponent(); 

     Debug.WriteLine("Download started"); 
     var file = Download().Result; // <-- Here's where it stucks 
     Debug.WriteLine("Download finished"); 

     player.Source = MediaSource.CreateFromStorageFile(file); 
     player.Play(); 
    } 

    private async Task<StorageFile> Download() 
    { 
     StorageFile destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
      "data.mp3", CreationCollisionOption.GenerateUniqueName); 
     BackgroundDownloader downloader = new BackgroundDownloader(); 
     DownloadOperation download = downloader.CreateDownload(new Uri(url), destinationFile); 
     await download.StartAsync(); 
     return destinationFile; 
    } 

私はPCと電話の両方でテストを行っており、プロセス全体が凍結しています。誰でもこれを修正することができますか、またはこれに代わる良い方法ですか?UWP BackgroundDownloaderがPC上でもフリーズする

答えて

0
  1. ダウンロード開始時に「AsTask」メソッドを呼び出すのを忘れた。
  2. 非同期コストラクターを使用できません。

    public MainPage() 
    { 
        this.InitializeComponent(); 
    } 
    
    private async void Page_Loaded(object sender, RoutedEventArgs e) 
    { 
        Debug.WriteLine("Download started"); 
        var file = await DownloadAsync(); 
        Debug.WriteLine("Download finished"); 
    
        player.Source = MediaSource.CreateFromStorageFile(file); 
        player.Play(); 
    } 
    
    private async Task<StorageFile> DownloadAsync() 
    { 
        StorageFile destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
         "data.mp3", CreationCollisionOption.GenerateUniqueName); 
        BackgroundDownloader downloader = new BackgroundDownloader(); 
        DownloadOperation download = downloader.CreateDownload(new Uri(url), destinationFile); 
        await download.StartAsync().AsTask(); 
        return destinationFile; 
    } 
    
関連する問題