I won't talk about how to dynamically call xap in Silverlight.
The problem is that many people always encounter exceptions when calling Alibaba Cloud.
Let me talk about my experience:
1. First, you should check whether the dynamic call code is correct.
This is the correct method I have found:
<UserControl x:Class="DynamicLoad.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <Border BorderBrush="Silver" BorderThickness="1" Height="271" HorizontalAlignment="Left" Name="border1" VerticalAlignment="Top" Width="400" /> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="166,277,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </Grid></UserControl>
Using system; using system. collections. generic; using system. LINQ; using system. net; using system. windows; using system. windows. controls; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. animation; using system. windows. shapes; using system. reflection; using system. windows. resources; using system. io; using system. XML; namespace dynamicload {Public p Artial class mainpage: usercontrol {public mainpage () {initializecomponent ();} private void button#click (Object sender, routedeventargs E) {try {// download becalled1.xap WebClient = new WebClient (); WebClient. openreadcompleted + = client_openreadcompleted; WebClient. openreadasync (New uri ("silverlightapplicationldaptest. xap ", urikind. relativeorabsolute);} catch (exception ex) {messagebo X. show (ex. message + "," + ex. stacktrace) ;}} void client_openreadcompleted (Object sender, openreadcompletedeventargs e) {If (E. error = NULL) {// other suites obtained through the getresourcestream method, which provides the streamresourceinfo xapresource = new streamresourceinfo (E. result, null); // extract the appmanifest response to streamresourceinfo appmanifest = application. getresourcestream (xapresource, new uri ("appmanifest. XAML ", urikind. rela (Tive); // create XML reader xmlreader = xmlreader. create (appmanifest. stream, new xmlreadersettings {dtdprocessing = dtdprocessing. ignore}); List <Assembly> List = new list <Assembly> (); While (! Xmlreader. EOF) {If (xmlreader. readtofollowing ("assemblypart") {string DLL = xmlreader. getattribute ("Source"); streamresourceinfo dllstreaminfo = application. getresourcestream (xapresource, new uri (DLL, urikind. relative); assemblypart part = new assemblypart (); Assembly = part. load (dllstreaminfo. stream); list. add (assembly) ;}} Assembly appassembly = List. singleordefault (a) =>. fullname. startswith ("silverlightapplicationldaptest") // The DLL name in the xap package must be the same); Type type = appassembly. getType ("silverlightapplicationldaptest. mainpage "); // call mainpage's contol usercontrol control = activator. createinstance (type) as usercontrol; border1.child = control ;}}}}
2. If you call xap and use web references and app-level resources, your main project must be referenced by the caller. Otherwise, the Web service node cannot be found or the resource cannot be found.
3. If you use WebClient, you cannot directly open HTML pages on the local machine. The server must be used, that is, the address is http: //