稳扎稳打Silverlight(31) - 2.0Tip/Trick之加载XAP, 加载XAML, 加载DLL, AppManifest.xaml文件说明

简介:
[索引页]
[源码下载]


稳扎稳打Silverlight(31) - 2.0Tip/Trick之加载XAP, 加载XAML, 加载DLL, AppManifest.xaml文件说明, 自定义鼠标指针


作者: webabcd


介绍
Silverlight 2.0 提示和技巧系列
  • 加载XAP - 加载指定的 xap 文件到当前的 Silverlight 应用程序中 
  • 加载XAML - 加载指定的 xaml 文件到当前的 Silverlight 应用程序中
  • 加载DLL - 加载指定的 dll 文件,并调用其中的方法或加载其中的控件
  • AppManifest.xaml文件说明 - 简要说明 AppManifest.xaml 文件内容中各个节点的作用 
  • 自定义鼠标指针 - 实现自定义的鼠标指针,即鼠标跟随


在线DEMO
http://webabcd.blog.51cto.com/1787395/342779


示例
1、在 Silverlight 程序中加载指定的 xap 文件
LoadXap.xaml
<UserControl x:Class="Silverlight20.Tip.LoadXap" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <Grid x:Name="LayoutRoot" Background="White"> 
                <StackPanel> 
                        <Button x:Name="load" Content="加载游戏 - 俄罗斯方块" Click="load_Click" Margin="5" /> 
                        <Grid x:Name="container" Margin="5" /> 
                </StackPanel> 
        </Grid> 
</UserControl>
 
LoadXap.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Windows.Resources; 
InBlock.gif using System.IO; 
InBlock.gif using System.Xml.Linq; 
InBlock.gif using System.Reflection; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Tip 
InBlock.gif
InBlock.gif         public partial  class LoadXap : UserControl 
InBlock.gif        { 
InBlock.gif                 public LoadXap() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void load_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        Uri uri =  new Uri( "YYTetris.xap", UriKind.Relative); 
InBlock.gif 
InBlock.gif                         // 用 WebClient 下载指定的 XAP 
InBlock.gif                        WebClient client =  new WebClient(); 
InBlock.gif                        client.OpenReadCompleted +=  new OpenReadCompletedEventHandler(client_OpenReadCompleted); 
InBlock.gif                        client.OpenReadAsync(uri); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void client_OpenReadCompleted( object sender, OpenReadCompletedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         /* 
InBlock.gif                         * StreamResourceInfo - 提供对通过 Silverlight 应用程序模型检索资源的支持 
InBlock.gif                         * AssemblyPart - 包含在 Silverlight 程序内的程序集 
InBlock.gif                         *         AssemblyPart.Load() - 加载指定的程序集到当前应用程序域中 
InBlock.gif                         * Application.GetResourceStream() - 对 zip 类型的文件自动解压缩 
InBlock.gif                         */
 
InBlock.gif 
InBlock.gif                         // YYTetris.xap 的 AppManifest.xaml 的信息如下 
InBlock.gif                         /*    
InBlock.gif                        <Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" EntryPointAssembly="YYTetris" EntryPointType="YYTetris.App" RuntimeVersion="2.0.31005.0"> 
InBlock.gif                            <Deployment.Parts> 
InBlock.gif                                <AssemblyPart x:Name="YYTetris" Source="YYTetris.dll" /> 
InBlock.gif                            </Deployment.Parts> 
InBlock.gif                        </Deployment>         
InBlock.gif                        */
 
InBlock.gif 
InBlock.gif                         // 可以通过 Deployment.Current 检索 AppManifest.xaml 中的 Deployment 对象 
InBlock.gif 
InBlock.gif                         // 取得 XAP 的 AppManifest.xaml 中的 Deployment.Parts 内的全部 AssemblyPart 节点信息 
InBlock.gif                        StreamResourceInfo resource = App.GetResourceStream( 
InBlock.gif                                 new StreamResourceInfo(e.Result,  null),    
InBlock.gif                                 new Uri( "AppManifest.xaml", UriKind.Relative)); 
InBlock.gif                         string resourceManifest =  new StreamReader(resource.Stream).ReadToEnd(); 
InBlock.gif                        List<XElement> assemblyParts = XDocument.Parse(resourceManifest).Root.Elements().Elements().ToList(); 
InBlock.gif 
InBlock.gif                        Assembly assembly =  null
InBlock.gif 
InBlock.gif                         foreach (XElement element  in assemblyParts) 
InBlock.gif                        { 
InBlock.gif                                 // 取出 AssemblyPart 的 Source 指定的 dll 
InBlock.gif                                 string source = element.Attribute( "Source").Value; 
InBlock.gif                                AssemblyPart assemblyPart =  new AssemblyPart(); 
InBlock.gif                                StreamResourceInfo streamInfo = App.GetResourceStream( 
InBlock.gif                                         new StreamResourceInfo(e.Result,  "application/binary"), 
InBlock.gif                                         new Uri(source, UriKind.Relative)); 
InBlock.gif 
InBlock.gif                                 if (source ==  "YYTetris.dll"
InBlock.gif                                        assembly = assemblyPart.Load(streamInfo.Stream); 
InBlock.gif                                 else 
InBlock.gif                                        assemblyPart.Load(streamInfo.Stream); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                         // 实例化 YYTetris.xap 的主函数 
InBlock.gif                        var type = assembly.GetType( "YYTetris.Page"); 
InBlock.gif                        var yyTetris = Activator.CreateInstance(type)  as UIElement; 
InBlock.gif 
InBlock.gif                         // 添加 YYTetris.xap 到指定的容器内,并更新 UI 
InBlock.gif                        container.Children.Add(yyTetris); 
InBlock.gif                        container.UpdateLayout(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
2、在 Silverlight 程序中加载指定的 xaml 文件
LoadXaml.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Windows.Resources; 
InBlock.gif using System.IO; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Tip 
InBlock.gif
InBlock.gif         public partial  class LoadXaml : UserControl 
InBlock.gif        { 
InBlock.gif                 public LoadXaml() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(LoadXaml_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void LoadXaml_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         // 获取指定的 Xaml 
InBlock.gif                        Uri uri =  new Uri( "/Silverlight20;component/Tip/XamlDemo.xaml", UriKind.Relative); 
InBlock.gif                        StreamResourceInfo resource = App.GetResourceStream(uri); 
InBlock.gif 
InBlock.gif                         using (StreamReader reader =  new StreamReader(resource.Stream)) 
InBlock.gif                        { 
InBlock.gif                                 // XamlReader.Load() - 加载 Xaml 
InBlock.gif                                UserControl control = 
InBlock.gif                                        System.Windows.Markup.XamlReader.Load(reader.ReadToEnd())  as UserControl; 
InBlock.gif 
InBlock.gif                                LayoutRoot.Children.Add(control); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
3、在 Silverlight 程序中加载指定的 dll 文件,并调用其中的方法或加载其中的控件
LoadDllDemo.Test.cs
InBlock.gif using System; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Ink; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif namespace LoadDllDemo 
InBlock.gif
InBlock.gif         public  class Test 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// Say Hello 的方法 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="name">名字</param> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  string Hello( string name) 
InBlock.gif                { 
InBlock.gif                         return  "Hello: " + name; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
LoadDll.xaml
<UserControl x:Class="Silverlight20.Tip.LoadDll" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <Grid x:Name="LayoutRoot" Background="White"> 
                 
                <TextBlock x:Name="lblResult" Margin="5" /> 
                 
        </Grid> 
</UserControl>
 
LoadDll.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Reflection; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Tip 
InBlock.gif
InBlock.gif         public partial  class LoadDll : UserControl 
InBlock.gif        { 
InBlock.gif                 public LoadDll() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(LoadDll_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void LoadDll_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        Uri uri =  new Uri( "LoadDllDemo.dll", UriKind.Relative); 
InBlock.gif 
InBlock.gif                         // 用 WebClient 下载指定的 DLL 
InBlock.gif                        WebClient client =  new WebClient(); 
InBlock.gif                        client.OpenReadCompleted +=  new OpenReadCompletedEventHandler(client_OpenReadCompleted); 
InBlock.gif                        client.OpenReadAsync(uri); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void client_OpenReadCompleted( object sender, OpenReadCompletedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         // 加载指定的 dll 
InBlock.gif                        AssemblyPart assemblyPart =  new AssemblyPart(); 
InBlock.gif                        Assembly assembly = assemblyPart.Load(e.Result); 
InBlock.gif 
InBlock.gif                         // 调用 LoadDllDemo.Test 的 Hello 方法,参数为 webabcd 
InBlock.gif                        Type type = assembly.GetType( "LoadDllDemo.Test"); 
InBlock.gif                         object obj = Activator.CreateInstance(type); 
InBlock.gif                        MethodInfo methodInfo = type.GetMethod( "Hello"); 
InBlock.gif                        lblResult.Text = methodInfo.Invoke(obj,  new  object[] {  "webabcd" }).ToString(); 
InBlock.gif 
InBlock.gif 
InBlock.gif                         // 注: 
InBlock.gif                         // AssemblyPart.Load() 用于把程序集加载到当前程序域 
InBlock.gif                         // 此特性可实现按需加载程序集(部署时,把这类的程序集从 xap 包中移除,用到时再调用指定发方法下载即可) 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
4、简要说明 AppManifest.xaml 文件内容中各个节点的作用
<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" EntryPointAssembly="Silverlight20" EntryPointType="Silverlight20.App" RuntimeVersion="2.0.31005.0"> 
    <Deployment.Parts> 
        <AssemblyPart x:Name="Silverlight20" Source="Silverlight20.dll" /> 
        <AssemblyPart x:Name="Microsoft.Windows.Controls" Source="Microsoft.Windows.Controls.dll" /> 
    </Deployment.Parts> 
</Deployment> 

一个 Silverlight 应用程序包(xap)内都会包含 AppManifest.xaml 文件,其内标识着打包的程序集和程序入口等信息 

Deployment - 根节点 
EntryPointAssembly - 程序入口的程序集 
EntryPointType - 程序入口的类型 
RuntimeVersion - 所需的 Silverlight 插件的版本 
AssemblyPart - 标识 Silverlight 程序包(xap)内的程序集。其中的 Source 属性用于标识程序集的路径
 
 
5、自定义鼠标指针,即鼠标跟随
CustomMouseCursor.xaml
<UserControl x:Class="Silverlight20.Tip.CustomMouseCursor" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <Grid x:Name="LayoutRoot" Background="Bisque"> 
                <Canvas x:Name="canvas"> 
                        <Ellipse x:Name="ellipse" Width="30" Height="30" Fill="Red" /> 
                </Canvas> 
        </Grid> 
</UserControl>
 
CustomMouseCursor.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Tip 
InBlock.gif
InBlock.gif         public partial  class CustomMouseCursor : UserControl 
InBlock.gif        { 
InBlock.gif                 public CustomMouseCursor() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.MouseMove +=  new MouseEventHandler(MouseFollow_MouseMove); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 // 自定义鼠标指针的实现如下。鼠标跟随也是同理的做 
InBlock.gif                 void MouseFollow_MouseMove( object sender, MouseEventArgs e) 
InBlock.gif                { 
InBlock.gif                        ellipse.SetValue(Canvas.LeftProperty, e.GetPosition(canvas).X - ellipse.ActualWidth / 2); 
InBlock.gif                        ellipse.SetValue(Canvas.TopProperty, e.GetPosition(canvas).Y - ellipse.ActualHeight / 2); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 



     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/343949 ,如需转载请自行联系原作者
相关文章

热门文章

最新文章