如何加载并调用Xap包呢?步骤如下,另附步骤注释(供参考)
DynamicInvokeXap.Xaml
1 <UserControl x:Class="Silverlight2._0.DynamicInvokeXap"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300">
4 <Grid x:Name="LayoutRoot" Background="White">
5 <StackPanel>
6 <TextBox x:Name="LastNameSearch" Width="200" Height="25"/>
7 <Button Click="Button_Click" Content="Submit Search" Width="200" Height="25"></Button>
8 <StackPanel x:Name="Panel1"></StackPanel>
9 </StackPanel>
10 </Grid>
11 </UserControl>
动态加载加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);
有关于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();
另附:动态加载的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
>
Step 2:
转换一下对象(XElement),不然就不能直接应用于Linq To Xml进行查询
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);
}
}
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();
Step 5:
1
相关数据源类
2
public
class
DynDataSource
{
3 public static List<CustomerInfo> GetCustomerInfos(String name)
4 {
5 List<CustomerInfo> custs = GetAllCustomerInfos();
6
7 var query = from c in custs
8 where name.ToUpper().StartsWith(c.Name)
9 select c;
10
11 return query.ToList();
12 }
13
14 internal static List<CustomerInfo> GetAllCustomerInfos()
15 {
16 List<CustomerInfo> customerInfos = new List<CustomerInfo>();
17 {
18 customerInfos.Add(new CustomerInfo { ID = 1, Name = "AAA" });
19 customerInfos.Add(new CustomerInfo { ID = 2, Name = "BBB" });
20 customerInfos.Add(new CustomerInfo { ID = 3, Name = "CCC" });
21 customerInfos.Add(new CustomerInfo { ID = 4, Name = "DDD" });
22 customerInfos.Add(new CustomerInfo { ID = 5, Name = "EEE" });
23 customerInfos.Add(new CustomerInfo { ID = 6, Name = "FFF" });
24 customerInfos.Add(new CustomerInfo { ID = 7, Name = "GGG" });
25 }
26 return customerInfos;
27 }
28}
29
30
public
class
CustomerInfo
31
{
32 public int ID { get; set; }
33 public String Name { get; set; }
34 }
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
>