怎么在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,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,System,Windows,new,using From: https://blog.51cto.com/u_696257/6151417