现在的位置: 首页 > 综合 > 正文

Windows Phone7 WebClient 图片下载

2013年05月23日 ⁄ 综合 ⁄ 共 8293字 ⁄ 字号 评论关闭

mainpage.xaml

View Code

 1 <phone:PhoneApplicationPage 
2 x:Class="PhoneApp2.MainPage"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
6 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9 mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
10 FontFamily="{StaticResource PhoneFontFamilyNormal}"
11 FontSize="{StaticResource PhoneFontSizeNormal}"
12 Foreground="{StaticResource PhoneForegroundBrush}"
13 SupportedOrientations="Portrait" Orientation="Portrait"
14 shell:SystemTray.IsVisible="True"
15
16 >
17
18 <!--LayoutRoot 是放置所有頁面的根資料格-->
19 <Grid x:Name="LayoutRoot" Background="Transparent">
20 <Grid.RowDefinitions>
21 <RowDefinition Height="Auto"/>
22 <RowDefinition Height="306*"/>
23 <RowDefinition Height="301*" />
24 </Grid.RowDefinitions>
25
26 <!--TitlePanel 包含應用程式的名稱和頁面標題-->
27 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
28 <TextBlock x:Name="ApplicationTitle" Text="图片下载" Style="{StaticResource PhoneTextNormalStyle}"/>
29 </StackPanel>
30
31 <!--ContentPanel - 其他內容置於此-->
32 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0" Grid.RowSpan="2">
33 <Grid.RowDefinitions>
34 <RowDefinition Height="406*" />
35 <RowDefinition Height="201*" />
36 </Grid.RowDefinitions>
37 <!--<Image
38 x:Name="img" Source="asd.jpg"
39 HorizontalAlignment="Left" Stretch="Fill" VerticalAlignment="Top" />-->
40 <Button Content="加载远程图片" Height="80" Margin="108,102,118,0" Name="button1" VerticalAlignment="Top" Click="button1_Click" Grid.Row="1" />
41 <TextBlock Height="30" HorizontalAlignment="Left" Margin="9,48,0,0" Name="textBlock1" Text="已下载:" VerticalAlignment="Top" Grid.Row="1" />
42 <TextBlock Height="30" HorizontalAlignment="Left" Margin="94,48,0,0" Name="TextBlock_FileSize" Text="" VerticalAlignment="Top" Grid.Row="1" />
43 <ProgressBar Margin="12,82,6,0" x:Name="Pro"
44 Maximum="100"
45 Value="0" Grid.Row="1" Height="27" VerticalAlignment="Top"></ProgressBar>
46 <ListBox Name="listBox_Img">
47
48 </ListBox>
49 <TextBlock Height="30" HorizontalAlignment="Left" Margin="9,14,0,0" Name="textBlock2" Text="正在读取:" VerticalAlignment="Top" Grid.Row="1" />
50 <TextBlock Height="30" HorizontalAlignment="Left" Margin="108,14,0,0" Name="textBlock_Jindu" Text="0/0" VerticalAlignment="Top" Grid.Row="1" />
51 </Grid>
52 </Grid>
53
54 <!--顯示 ApplicationBar 使用方式的程式碼範例-->
55 <!--<phone:PhoneApplicationPage.ApplicationBar>
56 <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
57 <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按鈕 1"/>
58 <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按鈕 2"/>
59 <shell:ApplicationBar.MenuItems>
60 <shell:ApplicationBarMenuItem Text="功能表項目 1"/>
61 <shell:ApplicationBarMenuItem Text="功能表項目 2"/>
62 </shell:ApplicationBar.MenuItems>
63 </shell:ApplicationBar>
64 </phone:PhoneApplicationPage.ApplicationBar>-->
65
66 </phone:PhoneApplicationPage>

 

mainpage.xaml.cs

View Code

  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Shapes;
13 using Microsoft.Phone.Controls;
14 using System.Threading;
15 using System.IO;
16 namespace PhoneApp2
17 {
18 public partial class MainPage : PhoneApplicationPage
19 {
20 string[] RemoteImg = new string[] {
21 "http://pic.dc.yesky.com/imagelist/08/07/7760633_7877.jpg",
22 "http://blog.ls-me.com/upload/2011/10/201110081521058118.jpg",
23 "http://blog.ls-me.com/upload/2011/10/201110082141356105.jpg"
24 };
25 // 建構函式
26 public MainPage()
27 {
28 InitializeComponent();
29 }
30
31 private void button1_Click(object sender, RoutedEventArgs e)
32 {
33 int Num = 1;
34 foreach (string Url in RemoteImg) {
35 this.Pro.Value = 0;
36 this.textBlock_Jindu.Text = string.Format("{0}/{1}",
37 Num.ToString(),
38 RemoteImg.Length.ToString()
39 );
40 DownLoadFile(Url);
41 Num++;
42 }
43
44 }
45 #region
46
47 /// <summary>
48 /// 下载文件流
49 /// </summary>
50 /// <param name="Url"></param>
51 private void DownLoadFile(string Url) {
52 WebClient Client = new WebClient();
53 Client.OpenReadAsync(new Uri(Url, UriKind.Absolute));
54 Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Client_DownloadProgressChanged);
55 Client.OpenReadCompleted += new OpenReadCompletedEventHandler(Client_OpenReadCompleted);
56 }
57 delegate void ShowDownloadProgressSize(int ProgressSize, long Size, long CountSize);
58 void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
59 {
60
61 Dispatcher.BeginInvoke(new ShowDownloadProgressSize(DownloadProgressSize), e.ProgressPercentage, e.BytesReceived, e.TotalBytesToReceive);
62 }
63 void DownloadProgressSize(int ProgressSize, long Size, long CountSize)
64 {
65 this.Pro.Value = ProgressSize;
66 this.TextBlock_FileSize.Text = string.Format("已下载{0}Kb,总大小{1}Kb",
67 (Size / 1024).ToString(),
68 (CountSize / 1024).ToString()
69 );
70 }
71
72
73 delegate void ShowDownloadCompleted(Stream _Stream);
74
75 /// <summary>
76 /// 下载完成,打开文件流
77 /// </summary>
78 /// <param name="sender"></param>
79 /// <param name="e"></param>
80 void Client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
81 {
82 Dispatcher.BeginInvoke(new ShowDownloadCompleted(OpenReadCompleted), e.Result);
83 }
84 void OpenReadCompleted(Stream _Stream)
85 {
86 if (!_Stream.Equals(null)) {
87
88 BitmapImage Bit = new BitmapImage();
89 Bit.SetSource(_Stream);
90 Image Image = new Image();
91 Image.Source = Bit;
92 ///放入listbox
93 this.listBox_Img.Items.Add(Image);
94 }
95
96
97 }
98 #endregion
99 }
100 }

有个问题,不知道怎么封装WebClient,直接调用方法,然后返回到UI上去。

异步读取的数据,不能按顺序执行,得到的都是空值,除非不断的循环判断,这样好麻烦,谁有更好的方法?

已经解决了

View Code

  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Shapes;
13 using Microsoft.Phone.Controls;
14 using System.Threading;
15 using System.IO;
16 namespace PhoneApp2
17 {
18 public partial class MainPage : PhoneApplicationPage
19 {
20 string[] RemoteImg = new string[] {
21 "http://pic.dc.yesky.com/imagelist/08/07/7760633_7877.jpg",
22 "http://blog.ls-me.com/upload/2011/10/201110081521058118.jpg",
23 "http://blog.ls-me.com/upload/2011/10/201110082141356105.jpg"
24 };
25 // 建構函式
26 public MainPage()
27 {
28 InitializeComponent();
29 }
30
31 private void button1_Click(object sender, RoutedEventArgs e)
32 {
33 this.listBox_Img.Items.Clear();
34 int Num = 1;
35 foreach (string Url in RemoteImg) {
36 this.Pro.Value = 0;
37 this.textBlock_Jindu.Text = string.Format("{0}/{1}",
38 Num.ToString(),
39 RemoteImg.Length.ToString()
40 );
41 Image _Img = new Image();
42 _Img.Source = new BitmapImage(new Uri("loading.png", UriKind.Relative));
43 this.listBox_Img.Items.Add(_Img);
44
45 DownLoadFile(Url, _Img);
46 Num++;
47 }
48 }
49 #region
50
51 /// <summary>
52 /// 下载文件流
53 /// </summary>
54 /// <param name="Url"></param>
55 private void DownLoadFile(string Url, Image _Img)
56 {
57 WebClient Client = new WebClient();
58
59 //userToken 标识,object类型
60 Client.OpenReadAsync(new Uri(Url, UriKind.Absolute),_Img);
61 Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Client_DownloadProgressChanged);
62 Client.OpenReadCompleted += new OpenReadCompletedEventHandler(Client_OpenReadCompleted);
63
64 }
65 delegate void ShowDownloadProgressSize(int ProgressSize, long Size, long CountSize);
66 void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
67 {
68
69 Dispatcher.BeginInvoke(new ShowDownloadProgressSize(DownloadProgressSize), e.ProgressPercentage, e.BytesReceived, e.TotalBytesToReceive);
70 }
71 void DownloadProgressSize(int ProgressSize, long Size, long CountSize)
72 {
73 this.Pro.Value = ProgressSize;
74 this.TextBlock_FileSize.Text = string.Format("已下载{0}Kb,总大小{1}Kb",
75 (Size / 1024).ToString(),
76 (CountSize / 1024).ToString()
77 );
78 }
79
80
81 delegate void ShowDownloadCompleted(Stream _Stream, Image _Img);
82
83 /// <summary>
84 /// 下载完成,打开文件流
85 /// </summary>
86 /// <param name="sender"></param>
87 /// <param name="e"></param>
88 void Client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
89 {
90
91 Dispatcher.BeginInvoke(new ShowDownloadCompleted(OpenReadCompleted), e.Result,(Image)e.UserState);
92 }
93 void OpenReadCompleted(Stream _Stream, Image _Img)
94 {
95 if (!_Stream.Equals(null)) {
96 BitmapImage Bit = new BitmapImage();
97 Bit.SetSource(_Stream);
98 _Img.Source = Bit;
99 }
100 }
101 #endregion
102 }
103 }

抱歉!评论已关闭.