[源碼下載]
作者:webabcd
介紹
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
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.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.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.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
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.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.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
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.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);
}
}
}
OK
[源碼下載]