In Silverlight, we often encounter the need to load other developed SilverlightProgramIn this section, we use WebClient to download the Silverlight program in the form of xap package, and then parse the Silverlight program into the Silverlight program using reflection. The procedure is as follows:
1. First, we use the openreadasync (URI) method of the WebClient class to read the xap file at a URI location and obtain the stream value of this xap.
2 • use application. getresourcestream (streamresourceinfo, Uri)Returns the resource file from a location in the specified zip/xap package. (This function has two parameters: the first parameter is the xap stream downloaded by WebClient from new streamresourceinfo (Stream. The second parameter is the dll uri of the file to be run at a location in the xap package .)
3 • The returned value obtained by running the function application. getresourcestream in the previous step is also a streamresourceinfo type value. Run the. Stream attribute to obtain its stream.
4 • The assemblypart. Load (Stream) method converts stream into an assembly that will then be loaded into the current application domain.
5 • (usercontrol) Assembly. createinstance (mainstr) searches for the specified type from this program according to the mainstr name, and then creates its instance using the system activator.
Next we will create a Silverlight application named sloadxap. In the clientbin folder of the sloadxap. Web project, add the radar xap package we created in section 29th, and write it.CodeThe xap package can be loaded dynamically.
Public Partial Class Mainpage: usercontrol
{
Public Mainpage ()
{
Initializecomponent ();
}
Private Void Button#click ( Object Sender, routedeventargs E)
{
// 1. Use WebClient to download the slrandarhittest. xap file for asynchronous reading.
WebClient appclient = New WebClient ();
Appclient. openreadasync ( New Uri ( " Slrandarhittest. xap " , Urikind. Relative ));
Appclient. openreadcompleted + = New Openreadcompletedeventhandler (appclient_openreadcompleted );
}
Void Appclient_openreadcompleted ( Object Sender, openreadcompletedeventargs E)
{
Createxapresource ( " Slrandar. dll " , " Slrandar. mainpage " , E. Result );
}
/// <Summary>
/// Create a reflection instance of the xap package and add it to the canvas.
/// </Summary>
/// <Param name = "dllstr"> DLL file of the compiled code in the xap package </Param>
/// <Param name = "mainstr"> Launch page to be instantiated </Param>
/// <Param name = "resultstream"> Data Stream downloaded from WebClient </Param>
Private Void Createxapresource ( String Dllstr, String Mainstr, stream resultstream)
{
// 2 •
// 3. Obtain the resource stream information provided by other packages.
Streamresourceinfo = Application. getresourcestream ( New Streamresourceinfo (resultstream As Stream, Null ), New Uri (dllstr, urikind. relativeorabsolute ));
// 4. Stream reflection
Assemblypart = New Assemblypart ();
Assembly = Assemblypart. Load (streamresourceinfo. Stream );
// 5 • (create an instance
VaR UC = (Usercontrol) Assembly. createinstance (mainstr );
Lroot. Children. Clear ();
Lroot. Children. Add (UC );
}
}
This example is written in vs2010 + Silverlight 4.0. To download the source code, click sloadxap.zip.