为什么使用WebClient不能下载~~是模拟器出问题吗?
本帖最后由 q8547957 于 2012-10-21 11:49:47 编辑 貌似不能下载那张图片~~总是不显示~~~要点击那个显示图片流的键~
下载的进度条也不会走~~
MainPage.xaml.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Text;
using System.IO;
namespace ImageToByte
{
public partial class MainPage : PhoneApplicationPage
{
// 构造函数
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
Uri img = new Uri("http://img6.ph.126.net/xMMHX5vxqilnYgG7qxSkYQ==/6597751463865557506.jpg", UriKind.RelativeOrAbsolute);
//使用WebClent下载图片
WebClient wc = new WebClient();
//注册OpenRead事件
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
//下载进度事件
wc.DownloadProgressChanged +=new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
//异步调用
wc.OpenWriteAsync(img);
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
void wc_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e)
{
Stream stream = e.Result;
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream);
showImage.Source = bitmap;
}
void wc_DownloadProgressChanged (object sender,DownloadProgressChangedEventArgs e)
{
this.Dispatcher.BeginInvoke(() =>
{
progressBar1.Value = e.ProgressPercentage;
});
}
}
}
<phone:PhoneApplicationPage
x:Class="ImageToByte.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="显示图片流" Height="72" HorizontalAlignment="Left" Margin="27,272,0,0" Name="button1" VerticalAlignment="Top" Width="183" Click="button1_Click" />
<Button Content="显示图片" Height="72" HorizontalAlignment="Right" Margin="0,272,38,0" Name="button2" VerticalAlignment="Top" Width="160" />
<Image Height="260" HorizontalAlignment="Left" Margin="6,6,0,0" Name="showImage" Stretch="Fill" VerticalAlignment="Top" Width="444" />
<ScrollViewer Height="201" HorizontalAlignment="Left" Margin="9,400,0,0" Name="scrollViewer1" VerticalAlignment="Top" Width="441">
<TextBlock Height="30" Name="showCode" Text="TextBlock" />
</ScrollViewer>
<ProgressBar Height="26" HorizontalAlignment="Left" Margin="6,350,0,0" Name="progressBar1" VerticalAlignment="Top" Width="460" />
</Grid>
</Grid>
<!--演示 ApplicationBar 用法的示例代码-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按钮 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按钮 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="菜单项 1"/>
<shell:ApplicationBarMenuItem Text="菜单项 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>