C# UWP BackgroundDownloader即使在PC上也会冻结 stringurl=”http://any_urls"; MediaPlayer=新的MediaPlayer(); 公共主页() { 初始化组件(); Debug.WriteLine(“下载已启动”); var file=Download().Result;// 开始下载时忘记调用“AsTask”方法
不能有异步构造函数C# UWP BackgroundDownloader即使在PC上也会冻结 stringurl=”http://any_urls"; MediaPlayer=新的MediaPlayer(); 公共主页() { 初始化组件(); Debug.WriteLine(“下载已启动”); var file=Download().Result;// 开始下载时忘记调用“AsTask”方法,c#,download,background,uwp,C#,Download,Background,Uwp,不能有异步构造函数 string url = "http://any_urls"; MediaPlayer player = new MediaPlayer(); public MainPage() { InitializeComponent(); Debug.WriteLine("Download started"); var file = Download().Result; // <-- Here's whe
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;
}
public主页()
{
this.InitializeComponent();
}
已加载专用异步无效页(对象发送方,RoutedEventArgs e)
{
Debug.WriteLine(“下载已启动”);
var file=await downloadsync();
Debug.WriteLine(“下载完成”);
player.Source=MediaSource.CreateFromStorageFile(文件);
player.Play();
}
私有异步任务下载异步()
{
StorageFile destinationFile=Wait ApplicationData.Current.LocalFolder.CreateFileAsync(
“data.mp3”,CreationCollisionOption.GenerateUniqueName);
BackgroundDownloader downloader=新的BackgroundDownloader();
DownloadOperation download=downloader.CreateDownload(新Uri(url),destinationFile);
等待下载.StartAsync().AsTask();
返回目标文件;
}
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;
}