<a href="http://webabcd.blog.51cto.com/1787395/342790" target="_blank">[索引頁]</a>
<a href="http://down.51cto.com/data/100302" target="_blank">[源碼下載下傳]</a>
穩紮穩打Silverlight(31) - 2.0Tip/Trick之加載XAP, 加載XAML, 加載DLL, AppManifest.xaml檔案說明, 自定義滑鼠指針
介紹
Silverlight 2.0 提示和技巧系列
加載XAP - 加載指定的 xap 檔案到目前的 Silverlight 應用程式中
加載XAML - 加載指定的 xaml 檔案到目前的 Silverlight 應用程式中
加載DLL - 加載指定的 dll 檔案,并調用其中的方法或加載其中的控件
AppManifest.xaml檔案說明 - 簡要說明 AppManifest.xaml 檔案内容中各個節點的作用
自定義滑鼠指針 - 實作自定義的滑鼠指針,即滑鼠跟随
線上DEMO
<a href="http://webabcd.blog.51cto.com/1787395/342779">http://webabcd.blog.51cto.com/1787395/342779</a>
示例
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
public partial class LoadXaml : UserControl
public LoadXaml()
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.Windows.Ink;
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"
<TextBlock x:Name="lblResult" Margin="5" />
LoadDll.xaml.cs
public partial class LoadDll : UserControl
public LoadDll()
this.Loaded += new RoutedEventHandler(LoadDll_Loaded);
void LoadDll_Loaded(object sender, RoutedEventArgs e)
Uri uri = new Uri("LoadDllDemo.dll", UriKind.Relative);
// 用 WebClient 下載下傳指定的 DLL
// 加載指定的 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"
<Grid x:Name="LayoutRoot" Background="Bisque">
<Canvas x:Name="canvas">
<Ellipse x:Name="ellipse" Width="30" Height="30" Fill="Red" />
</Canvas>
CustomMouseCursor.xaml.cs
public partial class CustomMouseCursor : UserControl
public CustomMouseCursor()
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
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/343949,如需轉載請自行聯系原作者