怎么在Silverlight 动态调用XAP 我就不说了
问题在于很多人调用时候总发生异常不知所云。
我就把我的经验说说:
1、首先你应该找找动态调用的代码有没有问题
这是我找的没有问题的方法:
<UserControl x:Class="DynamicLoad.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/Expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWIDth="400"> <GrID x:name="LayoutRoot" Background="White"> <border borderBrush="Silver" borderThickness="1" Height="271" HorizontalAlignment="left" name="border1" VerticalAlignment="top" WIDth="400" /> <button Content="button" Height="23" HorizontalAlignment="left" margin="166,277,0" name="button1" VerticalAlignment="top" WIDth="75" Click="button1_Click" /> </GrID></UserControl>
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;using System.windows.Resources;using System.IO;using System.Xml;namespace DynamicLoad{ public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private voID button1_Click(object sender,RoutedEventArgs e) { try { //下载BeCalled1.xap WebClIEnt webClIEnt = new WebClIEnt(); webClIEnt.OpenReadCompleted += clIEnt_OpenReadCompleted; webClIEnt.OpenReadAsync(new Uri("SilverlightApplicationLdaptest.xap",UriKind.relativeOrabsolute)); } catch (Exception ex) { MessageBox.Show(ex.Message + "," + ex.StackTrace); } } voID clIEnt_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e) { if (e.Error == null) { //透過 GetResourceStream 方法取得的其他套件,提供資源資料流資訊 StreamResourceInfo xapResource = new StreamResourceInfo(e.Result,null); //將其中的AppManifest讀取出來 StreamResourceInfo appManifest = Application.GetResourceStream(xapResource,new Uri("AppManifest.xaml",UriKind.relative)); //建立xml reader XmlReader xmlReader = XmlReader.Create(appManifest.Stream,new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore }); List<Assembly> List = new List<Assembly>(); while (!xmlReader.EOF) { if (xmlReader.ReadToFollowing("AssemblyPart")) { string dll = xmlReader.GetAttribute("Source"); StreamResourceInfo dllStreamInfo = Application.GetResourceStream(xapResource,new Uri(dll,UriKind.relative)); AssemblyPart part = new AssemblyPart(); Assembly assembly = part.Load(dllStreamInfo.Stream); List.Add(assembly); } } Assembly appAssembly = List.SingleOrDefault((a) => a.Fullname.StartsWith("SilverlightApplicationLdaptest") //这个和XAP包 里面DLL名字必须相同 ); Type type = appAssembly.GetType("SilverlightApplicationLdaptest.MainPage"); //呼叫MainPage那Contol UserControl control = Activator.CreateInstance(type) as UserControl; border1.Child = control; } } }}
2、 你调用XAP 如果用到Web引用 还有App 级别的资源,那么你的主项目就是 调用方必须也要引用。否则会报找不到web服务节点或者找不到资源的问题。
3、如果你用到WebClIEnt那么是无法在本机就是直接打开HTML页面方式使用。必须使用服务器,就是地址是http://这种
总结以上是内存溢出为你收集整理的Silverlight 动态调用XAP 发生异常解决方法全部内容,希望文章能够帮你解决Silverlight 动态调用XAP 发生异常解决方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)