如何加载并调用Xap包呢?步骤如下,另附步骤注释(供参考)
DynamicInvokeXap.Xaml
>
动态加载加Xap代码:
1 WebClient webClient = new WebClient();
2 webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
3 webClient.OpenReadAsync(new Uri("http://localhost/Silverlight.Web/ClientBin/SilverlightAttatch.xap"), UriKind.Relative);
2 webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
3 webClient.OpenReadAsync(new Uri("http://localhost/Silverlight.Web/ClientBin/SilverlightAttatch.xap"), UriKind.Relative);
有关于Uri地址:(这里有一个目录问题要注意一下)
1.如果你aspx在项目根目录,Uri("SilverlightAttatch.xap"),即可
2.如果你aspx在目录中 如:Project -- Folder -- X.aspx
建议直接用地址访问,不然会UriFormatException
new Uri("http://localhost/Silverlight.Web/ClientBin/SilverlightAttatch.xap")
再看看webClient_OpenReadCompleted事件是如何加载dll,以及相关dll调用.
Step 1: (具体相关内容参见:另附)
//加载AppManifest.xaml,把需要加载(布署)的Dll配置到Appmanifest.xaml中
String appManifest = new StreamReader(Application.GetResourceStream(
new StreamResourceInfo(e.Result, null),
new Uri("AppManifest.xaml", UriKind.Relative)
).Stream).ReadToEnd();
String appManifest = new StreamReader(Application.GetResourceStream(
new StreamResourceInfo(e.Result, null),
new Uri("AppManifest.xaml", UriKind.Relative)
).Stream).ReadToEnd();
另附:动态加载的appmanifest.xaml内容
<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" EntryPointAssembly="SilverlightAttatch" EntryPointType="SilverlightAttatch.App" RuntimeVersion="2.0.31005.0">
<Deployment.Parts>
<AssemblyPart x:Name="SilverlightAttatch" Source="SilverlightAttatch.dll" />
<AssemblyPart x:Name="System.Windows.Controls.Data" Source="System.Windows.Controls.Data.dll" />
<AssemblyPart x:Name="System.Windows.Controls" Source="System.Windows.Controls.dll" />
</Deployment.Parts>
</Deployment>
<Deployment.Parts>
<AssemblyPart x:Name="SilverlightAttatch" Source="SilverlightAttatch.dll" />
<AssemblyPart x:Name="System.Windows.Controls.Data" Source="System.Windows.Controls.Data.dll" />
<AssemblyPart x:Name="System.Windows.Controls" Source="System.Windows.Controls.dll" />
</Deployment.Parts>
</Deployment>
Step 2:
转换一下对象(XElement),不然就不能直接应用于Linq To Xml进行查询
XElement deploy = XDocument.Parse(appManifest).Root;
List<XElement> parts = ( from assemblyParts in deploy.Elements().Elements()
select assemblyParts
).ToList();
XElement deploy = XDocument.Parse(appManifest).Root;
List<XElement> parts = ( from assemblyParts in deploy.Elements().Elements()
select assemblyParts
).ToList();
Step 3:
这里对appmanifest.xaml中的进行一下loop
目的:1.是SilverlightAttatch.dll作为Assembly进行处理,直接load
2.对于其他dll作为assembly的一些reference,直接进行AssemblyPart的load
Assembly assebmly = null;
foreach (XElement xe in parts) {
String source = xe.Attribute("Source").Value;
AssemblyPart asmPart = new AssemblyPart();
StreamResourceInfo streamInfo = Application.GetResourceStream(
new StreamResourceInfo(e.Result, "application/binary"),
new Uri(source, UriKind.Relative));
if (source.Equals("SilverlightAttatch.dll")){
assebmly = asmPart.Load(streamInfo.Stream);
}
else{
asmPart.Load(streamInfo.Stream);
}
}
目的:1.是SilverlightAttatch.dll作为Assembly进行处理,直接load
2.对于其他dll作为assembly的一些reference,直接进行AssemblyPart的load
Assembly assebmly = null;
foreach (XElement xe in parts) {
String source = xe.Attribute("Source").Value;
AssemblyPart asmPart = new AssemblyPart();
StreamResourceInfo streamInfo = Application.GetResourceStream(
new StreamResourceInfo(e.Result, "application/binary"),
new Uri(source, UriKind.Relative));
if (source.Equals("SilverlightAttatch.dll")){
assebmly = asmPart.Load(streamInfo.Stream);
}
else{
asmPart.Load(streamInfo.Stream);
}
}
Step 4:
到数据源的查询,并返回数据到UI容器
Panel1.DataContext = DynDataSource.GetCustomerInfos(LastNameSearch.Text);
创建一个UserControl实例
UIElement element = assebmly.CreateInstance("SilverlightAttatch.Page") as UIElement;
加载相应的Silverlight's UserControl
Panel1.Children.Add(element);
更新UI
Panel1.UpdateLayout();
Panel1.DataContext = DynDataSource.GetCustomerInfos(LastNameSearch.Text);
创建一个UserControl实例
UIElement element = assebmly.CreateInstance("SilverlightAttatch.Page") as UIElement;
加载相应的Silverlight's UserControl
Panel1.Children.Add(element);
更新UI
Panel1.UpdateLayout();
Step 5:
1
相关数据源类
2
}
2
Step 6:(需要加载的Xap包中Page.xaml)
1 <UserControl x:Class="SilverlightAttatch.Page"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
5 <Grid x:Name="LayoutRoot" Background="White">
6 <data:DataGrid x:Name="SearchResults" AutoGenerateColumns="True" ItemsSource="{Binding}"></data:DataGrid>
7 </Grid>
8 </UserControl>
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
5 <Grid x:Name="LayoutRoot" Background="White">
6 <data:DataGrid x:Name="SearchResults" AutoGenerateColumns="True" ItemsSource="{Binding}"></data:DataGrid>
7 </Grid>
8 </UserControl>