介绍
Silverlight 2.0 提示和技巧系列
- 加载XAP - 加载指定的 xap 文件到当前的 Silverlight 应用程序中
- 加载XAML - 加载指定的 xaml 文件到当前的 Silverlight 应用程序中
- 加载DLL - 加载指定的 dll 文件,并调用其中的方法或加载其中的控件
- AppManifest.xaml文件说明 - 简要说明 AppManifest.xaml 文件内容中各个节点的作用
- 自定义鼠标指针 - 实现自定义的鼠标指针,即鼠标跟随
在线DEMO
http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html
示例
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
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
System.Windows.Resources;
using
System.IO;
using
System.Xml.Linq;
using
System.Reflection;
namespace
Silverlight20.Tip
{
public partial class LoadXap : UserControl
{
public LoadXap()
{
InitializeComponent();
}
private void load_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("YYTetris.xap", UriKind.Relative);
// 用 WebClient 下载指定的 XAP
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(uri);
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
/**//*
* StreamResourceInfo - 提供对通过 Silverlight 应用程序模型检索资源的支持
* AssemblyPart - 包含在 Silverlight 程序内的程序集
* AssemblyPart.Load() - 加载指定的程序集到当前应用程序域中
* Application.GetResourceStream() - 对 zip 类型的文件自动解压缩
*/
// YYTetris.xap 的 AppManifest.xaml 的信息如下
/**//*
<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">
<Deployment.Parts>
<AssemblyPart x:Name="YYTetris" Source="YYTetris.dll" />
</Deployment.Parts>
</Deployment>
*/
// 可以通过 Deployment.Current 检索 AppManifest.xaml 中的 Deployment 对象
// 取得 XAP 的 AppManifest.xaml 中的 Deployment.Parts 内的全部 AssemblyPart 节点信息
StreamResourceInfo resource = App.GetResourceStream(
new StreamResourceInfo(e.Result, null),
new Uri("AppManifest.xaml", UriKind.Relative));
string resourceManifest = new StreamReader(resource.Stream).ReadToEnd();
List<XElement> assemblyParts = XDocument.Parse(resourceManifest).Root.Elements().Elements().ToList();
Assembly assembly = null;
foreach (XElement element in assemblyParts)
{
// 取出 AssemblyPart 的 Source 指定的 dll
string source = element.Attribute("Source").Value;
AssemblyPart assemblyPart = new AssemblyPart();
StreamResourceInfo streamInfo = App.GetResourceStream(
new StreamResourceInfo(e.Result, "application/binary"),
new Uri(source, UriKind.Relative));
if (source == "YYTetris.dll")
assembly = assemblyPart.Load(streamInfo.Stream);
else
assemblyPart.Load(streamInfo.Stream);
}
// 实例化 YYTetris.xap 的主函数
var type = assembly.GetType("YYTetris.Page");
var yyTetris = Activator.CreateInstance(type) as UIElement;
// 添加 YYTetris.xap 到指定的容器内,并更新 UI
container.Children.Add(yyTetris);
container.UpdateLayout();
}
}
}
2、在 Silverlight 程序中加载指定的 xaml 文件
LoadXaml.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
System.Windows.Resources;
using
System.IO;
namespace
Silverlight20.Tip
{
public partial class LoadXaml : UserControl
{
public LoadXaml()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(LoadXaml_Loaded);
}
void LoadXaml_Loaded(object sender, RoutedEventArgs e)
{
// 获取指定的 Xaml
Uri uri = new Uri("/Silverlight20;component/Tip/XamlDemo.xaml", UriKind.Relative);
StreamResourceInfo resource = App.GetResourceStream(uri);
using (StreamReader reader = new StreamReader(resource.Stream))
{
// XamlReader.Load() - 加载 Xaml
UserControl control =
System.Windows.Markup.XamlReader.Load(reader.ReadToEnd()) as UserControl;
LayoutRoot.Children.Add(control);
}
}
}
}
3、在 Silverlight 程序中加载指定的 dll 文件,并调用其中的方法或加载其中的控件
LoadDllDemo.Test.cs
using
System;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Ink;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
namespace
LoadDllDemo
{
public class Test
{
/**//// <summary>
/// Say Hello 的方法
/// </summary>
/// <param name="name">名字</param>
/// <returns></returns>
public string Hello(string name)
{
return "Hello: " + name;
}
}
}
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
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
System.Reflection;
namespace
Silverlight20.Tip
{
public partial class LoadDll : UserControl
{
public LoadDll()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(LoadDll_Loaded);
}
void LoadDll_Loaded(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("LoadDllDemo.dll", UriKind.Relative);
// 用 WebClient 下载指定的 DLL
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(uri);
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
// 加载指定的 dll
AssemblyPart assemblyPart = new AssemblyPart();
Assembly assembly = assemblyPart.Load(e.Result);
// 调用 LoadDllDemo.Test 的 Hello 方法,参数为 webabcd
Type type = assembly.GetType("LoadDllDemo.Test");
object obj = Activator.CreateInstance(type);
MethodInfo methodInfo = type.GetMethod("Hello");
lblResult.Text = methodInfo.Invoke(obj, new object[] { "webabcd" }).ToString();
// 注:
// AssemblyPart.Load() 用于把程序集加载到当前程序域
// 此特性可实现按需加载程序集(部署时,把这类的程序集从 xap 包中移除,用到时再调用指定发方法下载即可)
}
}
}
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
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;
namespace
Silverlight20.Tip
{
public partial class CustomMouseCursor : UserControl
{
public CustomMouseCursor()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(MouseFollow_MouseMove);
}
// 自定义鼠标指针的实现如下。鼠标跟随也是同理的做
void MouseFollow_MouseMove(object sender, MouseEventArgs e)
{
ellipse.SetValue(Canvas.LeftProperty, e.GetPosition(canvas).X - ellipse.ActualWidth / 2);
ellipse.SetValue(Canvas.TopProperty, e.GetPosition(canvas).Y - ellipse.ActualHeight / 2);
}
}
}