[源码下载]
稳扎稳打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://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()
@H_727_403@
{InitializeComponent();
@H_181_419@
}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);
@H_181_419@
}voID clIEnt_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
/**//*
* StreamResourceInfo - 提供对通过 Silverlight 应用程序模型检索资源的支持
* AssemblyPart - 包含在 Silverlight 程序内的程序集
* AssemblyPart.Load() - 加载指定的程序集到当前应用程序域中
* Application.GetResourceStream() - 对 zip 类型的文件自动解压缩
@H_181_419@
*/// 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>
@H_181_419@
*/// 可以通过 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);
@H_181_419@
}// 实例化 YYTetris.xap 的主函数
var type = assembly.GetType("YYTetris.Page");
var yyTetris = Activator.CreateInstance(type) as UIElement;
// 添加 YYTetris.xap 到指定的容器内,并更新 UI
container.Children.Add(yyTetris);
container.UpdateLayout();
@H_181_419@
}@H_181_419@
}}
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);
}
}
}
@H_291_1419@
}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;
}
}
@H_291_1419@
}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");
@H_502_2285@
lblResult.Text = methodInfo.Invoke(obj, new object[] { "webabcd" }).ToString();// 注:
// AssemblyPart.Load() 用于把程序集加载到当前程序域
// 此特性可实现按需加载程序集(部署时,把这类的程序集从 xap 包中移除,用到时再调用指定发方法下载即可)
}
}
@H_291_1419@
}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)
@H_301_2924@{
ellipse.SetValue(Canvas.leftProperty, e.Getposition(canvas).X - ellipse.ActualWIDth / 2);
ellipse.SetValue(Canvas.topProperty, e.Getposition(canvas).Y - ellipse.ActualHeight / 2);
}
}
@H_291_1419@
}OK
[源码下载] 总结
以上是内存溢出为你收集整理的稳扎稳打Silverlight(31) - 2.0Tip/Trick之加载XAP, 加载XAML, 加载DLL, AppManifest.xaml文件说明, 自定义鼠标指针全部内容,希望文章能够帮你解决稳扎稳打Silverlight(31) - 2.0Tip/Trick之加载XAP, 加载XAML, 加载DLL, AppManifest.xaml文件说明, 自定义鼠标指针所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)