Silverlight.WebClient下载

System.Net.WebClient 是我们在客户端常用下载对象,image也是sl中出镜率较高的控件
再加上一个MediaElement控件,让我们就此实现一个会动的silverlightLOGO吧~


xaml的组成很简单:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width
="400" Height="300">
    
<Canvas Background="White">
        
<MediaElement Width="50" Height="50" AutoPlay="True" x:Name="myMp"/>
        
<Image x:Name="myImage" Width="150" Height="50" Canvas.Left="60" ></Image>
    
</Canvas>
</UserControl>

接下来我们将 Image绑定图片:
new BitmapImage(new Uri("Properties/microsoft_silverlight_c - Copy.jpg", UriKind.Relative));

随后声明WebClient对象和注册委托事件:
webClient.openReadAsync()方法参数
1.位于服务器上的数据源的路径.
2.给与用户一个标识对本次访问服务器.

 WebClient wc = new WebClient();
 wc.OpenReadCompleted 
+= new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
 wc.OpenReadAsync(
new Uri("SilverlightLogo.zip", UriKind.Relative), "SilverlightLogo.wmv"); 

        
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        
{
            //读取数据完成

        }

当下载数据完成后回调openreadcompleted方法
我们可以从参数e中得到有用的数据

 String sURI = e.UserState as String;
 StreamResourceInfo zipResourceInfo 
= new StreamResourceInfo(e.Result as Stream, null);
 StreamResourceInfo imageResourceInfo 
= Application.GetResourceStream(zipResourceInfo, new Uri(sURI, UriKind.RelativeOrAbsolute));
 myMp.SetSource(imageResourceInfo.Stream);

就这样我们已将一个会动的silverlightlogo展现给用户了,很简单吧~
Source code: download.

posted @ 2008-07-17 17:25  王博_Nick  Views(3060)  Comments(2Edit  收藏  举报