Silverlight多个xap之间导航 silverlight整站程序动态加载xap包
XAML代码:
1 <Border Name="Frame" Grid.Column="1" Style="{StaticResource ContentBorderStyle}">
2 <Grid Grid.Column="1" Name="gridFrame">
3
4 </Grid>
5 </Border>
后台代码:
1 private void LoadSolutionManagement()
2 {
3 WebClient Appclient = new WebClient();
4 Appclient.OpenReadAsync(new Uri("UI.xap", UriKind.Relative));
5 Appclient.OpenReadCompleted += new OpenReadCompletedEventHandler(Appclient_OpenReadCompleted);
6 }
7
8 private void Appclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
9 {
10 CreateXAPResource("HuaweiSoft.COSTSW.UI.dll", "HuaweiSoft.COSTSW.UI.Views.ProjectContent", e.Result);
11 }
12
13 ///<summary>
14 /// 创建XAP包的反射实例并且加装到Grid中
15 ///</summary>
16 ///<param name="dllStr">XAP包中的编译完成的运行代码的DLL文件</param>
17 ///<param name="mainStr">需要实例化的启动页面</param>
18 ///<param name="resultStream">使用WebClient下载到的数据流</param>
19 private void CreateXAPResource(string dllStr, string mainStr, Stream resultStream)
20 {
21 StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new StreamResourceInfo(resultStream as Stream, null), new Uri(dllStr, UriKind.RelativeOrAbsolute));
22 // 对Stream进行反射
23 AssemblyPart assemblyPart = new AssemblyPart();
24 Assembly assembly = assemblyPart.Load(streamResourceInfo.Stream);
25 // 创建实例
26 var uc = (UserControl)assembly.CreateInstance(mainStr);
27 gridFrame.Children.Clear();
28 gridFrame.Children.Add(uc);
29 }