图片下载

private async void Button_Click(object sender, RoutedEventArgs e)
        {
            //在这里触发下载事件
            //List<ServiceReference1.Pic> jokes = await this.getFileContentAsync();
            //imagepoll.Orientation = Orientation.Vertical;
            //imagepoll.Width = 800;
            //foreach (ServiceReference1.Pic pic in jokes)
            //{
            //    StackPanel panel = new StackPanel();
            //    panel.Orientation = Orientation.Horizontal;

            //    ImageBrush imageBrush = new ImageBrush();
            //    imageBrush.ImageSource = new BitmapImage(new Uri(pic.URL));
            //    Image image = new Image();
            //    image.Source = imageBrush.ImageSource;
            //    image.Margin = new Thickness(0, 0, 10, 0);
            //    image.Width = 100;

            //    panel.Children.Add(image);

            //    TextBlock block = new TextBlock();
            //    block.Text = pic.URL;
            //    block.VerticalAlignment = VerticalAlignment.Center;
            //    panel.Children.Add(block);
            //    this.imagepoll.Children.Add(panel);
            //    panel.RightTapped += Mource_Move;
            //}
            //检验是否下载成功
            var root = Windows.Storage.ApplicationData.Current.LocalFolder;
            var path = await root.GetFolderAsync("filla");
            var filename = await path.GetFileAsync("aaa.jpg");
            StackPanel panel = new StackPanel();
            panel.Orientation = Orientation.Horizontal;

            ImageBrush imageBrush = new ImageBrush();
            imageBrush.ImageSource = new BitmapImage(new Uri(filename.Path));
            Image image = new Image();

            image.Source = imageBrush.ImageSource;
            image.Margin = new Thickness(0, 0, 10, 0);
            image.Width = 100;

            panel.Children.Add(image);

            TextBlock block = new TextBlock();
            block.Text = "aaa";
            block.VerticalAlignment = VerticalAlignment.Center;
            panel.Children.Add(block);
            this.imagepoll.Children.Add(panel);
            MessageDialog dialog = new MessageDialog(filename.Path);
            dialog.ShowAsync();
        }
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void Mource_Move(object sender, RoutedEventArgs e)
        {
            StackPanel panel = (StackPanel)sender;
            TextBlock block = (TextBlock)panel.Children[1];
            string message = block.Text;

            var root = Windows.Storage.ApplicationData.Current.LocalFolder;
            var path = await root.CreateFolderAsync("filla", CreationCollisionOption.OpenIfExists);
            var filename = await path.CreateFileAsync("aaa.jpg");


            BackgroundDownloader downloader = new BackgroundDownloader();
            DownloadOperation download = downloader.CreateDownload(new Uri(message), filename);
            await download.StartAsync();
            ResponseInformation response = download.GetResponseInformation();

            MessageDialog dialog = new MessageDialog(response.StatusCode.ToString());
            dialog.ShowAsync();

        }

 

你可能感兴趣的:(下载)