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.

Tag标签: Silverlight
posted @ 2008-07-17 17:25 王博-sonic 阅读(405) 评论(1)  编辑 收藏 网摘 所属分类: Silverlight

  回复  引用  查看    
#1楼2008-07-28 17:19 | 涵舍愚人      
还是沙发
发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 1245338




相关文章:

相关链接: