Downloading Media File programmatically in Windows Phone 8

泪湿孤枕 提交于 2019-12-04 08:26:36

For downloading binary data, use [WebClient.OpenReadAsync][1]. You can use it to download data other than string data.

var webClient = new WebClient();
webClient.OpenReadCompleted += OnOpenReadCompleted;
webClient.OpenReadAsync(new Uri("...", UriKind.Absolute));

private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{

}

Save this in your toolbox :)

    public static Task<Stream> DownloadFile(Uri url)
    {
        var tcs = new TaskCompletionSource<Stream>();
        var wc = new WebClient();
        wc.OpenReadCompleted += (s, e) =>
        {
            if (e.Error != null) tcs.TrySetException(e.Error);
            else if (e.Cancelled) tcs.TrySetCanceled();
            else tcs.TrySetResult(e.Result);
        };
        wc.OpenReadAsync(url);
        return tcs.Task;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!