Loading other xap in silverlight3

Source: Internet
Author: User

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 :}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.