silverlight
发布时间:2022-05-04 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了稳扎稳打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 - 加载指定的 xa
[索引页]
[源码下载]
稳扎稳打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 文件内容中各个节点的作用
- 自定义鼠标指针 - 实现自定义的鼠标指针,即鼠标跟随
@H_419_30@
在线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://scheR_491_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_491_11845@as.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_802_403@
{
initializeComponent();
@H_940_419@
}
private void load_Click(object sender, routedEventArgs E)
{
Uri uri = new Uri("YYTetris.xap", UriKind.RelativE);
// 用 WebClient 下载指定的 XAP
WebClient client = new WebClient();
client.openReadCoR_491_11845@pleted += new OpenReadCoR_491_11845@pletedEventHandler(client_OpenReadCoR_491_11845@pleted);
client.openReadAsync(uri);
@H_940_419@
}
void client_OpenReadCoR_491_11845@pleted(object sender, OpenReadCoR_491_11845@pletedEventArgs E)
{
/**//*
* StreamresourceInfo - 提供对通过 Silverlight 应用程序模型检索资源的支持
* AssemblyPart - 包含在 Silverlight 程序内的程序集
* AssemblyPart.Load() - 加载指定的程序集到当前应用程序域中
* Application.GetresourceStream() - 对 zip 类型的文件自动解压缩
@H_940_419@
*/
// YYTetris.xap 的 AppManifest.xaml 的信息如下
/**//*
<Deployment xmlns="http://scheR_491_11845@as.microsoft.com/client/2007/deployment" xmlns:x="http://scheR_491_11845@as.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_940_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_940_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_940_419@
}
@H_940_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
@H_276_1450@
{
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_128_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
{
@H_674_1935@
@H_478_1944@/**//// <sumMary>
/// Say Hello 的方法
/// </sumMary>
/// <param NAME="name">名字</param>
/// <returns></returns>
public String Hello(String Name)
{
return "Hello: " + NAME;
}
}
@H_128_1419@
}
LoadDll.xaml
<
UserControl
x:Class
="Silverlight20.Tip.LoadDll"
xmlns
="http://scheR_491_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_491_11845@as.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
@H_388_2450@{
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.openReadCoR_491_11845@pleted += new OpenReadCoR_491_11845@pletedEventHandler(client_OpenReadCoR_491_11845@pleted);
client.openReadAsync(uri);
}
void client_OpenReadCoR_491_11845@pleted(object sender, OpenReadCoR_491_11845@pletedEventArgs 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[] @H_944_2828@{ "webabcd" }).ToString();
// 注:
// AssemblyPart.Load() 用于把程序集加载到当前程序域
// 此特性可实现按需加载程序集(部署时,把这类的程序集从 xap 包中移除,用到时再调用指定发方法下载即可)
}
}
@H_128_1419@
}
4、简要说明 AppManifest.xaml 文件内容中各个节点的作用
<
Deployment
xmlns
="http://scheR_491_11845@as.microsoft.com/client/2007/deployment"
xmlns:x
="http://scheR_491_11845@as.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://scheR_491_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_491_11845@as.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()
@H_945_3489@{
initializeComponent();
this.MouseMove += new MouseEventHandler(MouseFollow_MouseMovE);
}
// 自定义鼠标指针的实现如下。鼠标跟随也是同理的做
void MouseFollow_MouseMove(object sender, MouseEventArgs E)
@H_197_3572@
{
ellipse.SETVALue(Canvas.LeftProperty, e.GetPosition(canvas).X - ellipse.ActualWidth / 2);
ellipse.SETVALue(Canvas.TopProperty, e.GetPosition(canvas).Y - ellipse.ActualHeight / 2);
}
}
@H_128_1419@
}
OK
[源码下载]
大佬总结
以上是大佬教程为你收集整理的稳扎稳打Silverlight(31) - 2.0Tip/Trick之加载XAP, 加载XAML, 加载DLL, AppManifest.xaml文件说明, 自定义鼠标指针全部内容,希望文章能够帮你解决稳扎稳打Silverlight(31) - 2.0Tip/Trick之加载XAP, 加载XAML, 加载DLL, AppManifest.xaml文件说明, 自定义鼠标指针所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。