Sometimes we always want to use xap as the base point to achieve continuous integration when designing an integrated SL system. So what should we do?
Solution:
1. First, we need to use a method to extract the Assembly from the source Resource:
1: Assembly loadassemblyfromxap (Stream packagestream, string assemblyname)
2 :{
3: streamresourceinfo resouceinfo = new streamresourceinfo (packagestream, "application/binary ");
4: stream mainfeststream = application. getresourcestream (resouceinfo, new uri ("appmanifest. XAML", urikind. Relative). stream;
5: String appmanifeststring = new streamreader (mainfeststream). readtoend ();
6:
7: xelement deploymentroot = xdocument. parse (appmanifeststring). root;
8: List <xelement> deploymentparts = (from assemblyparts in deploymentroot. Elements (). Elements () Select assemblyparts). tolist ();
9:
10: Assembly targetassembly = NULL;
11:
12: foreach (xelement in deploymentparts)
13 :{
14: String source = xelement. Attribute ("Source"). value;
15: If (Source = assemblyname)
16 :{
17: streamresourceinfo streaminfo = application. getresourcestream (resouceinfo, new uri (source, urikind. Relative ));
18: assemblypart asmpart = new assemblypart ();
19: targetassembly = asmpart. Load (streaminfo. Stream );
20 :}
21 :}
22: Return targetassembly;
23 :}
2. Use a WebClient to download the specified xap file. After the download is complete, use the preceding method to complete the loaded file as a uielement, so that we can use it.
1: void mainpage_loaded (Object sender, routedeventargs E)
2 :{
3: // Add the location of an xap File
4: URI address = new uri ("http: // localhost: 4456/clientbin/exproject. xap ");
5: // A WebClient of the Instance
6: WebClient = new WebClient ();
7: // register a download completion event
8: WebClient. openreadcompleted + = new openreadcompletedeventhandler (webclient_openreadcompleted );
9: // start download
10: WebClient. openreadasync (Address );
11 :}
3. After the download is complete, use the loadassemblyfromxap method to restore xpa to uielement:
1: void webclient_openreadcompleted (Object sender, openreadcompletedeventargs E)
2 :{
3: stream = application. getresourcestream (
4: New streamresourceinfo (E. Result, null ),
5: New uri ("appmanifest. XAML", urikind. Relative). stream;
6: String appmanifeststring = new streamreader (Stream). readtoend ();
7:
8: Assembly = loadassemblyfromxap (E. result, "exproject. dll"); // other project name. dll
9: uielement element = assembly. createinstance ("exproject. mainpage") as uielement; // other project name. mainpage
10: This. contaner. Children. Add (element );
11 :}