How does one solve an exception when Silverlight dynamically calls xap?

Source: Internet
Author: User
Tags xml reader silverlight

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

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.