Silverlight 动态调用XAP 发生异常解决方法

  怎么在Silverlight 动态调用XAP  我就不说了

  问题在于很多人调用时候总发生异常不知所云。

  我就把我的经验说说:

 1、首先你应该找找动态调用的代码有没有问题

 这是我找的没有问题的方法:


<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 partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                //下载BeCalled1.xap 

                WebClient webClient = new WebClient();

                webClient.OpenReadCompleted += client_OpenReadCompleted;

                webClient.OpenReadAsync(new Uri("SilverlightApplicationLdaptest.xap", UriKind.RelativeOrAbsolute));

            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message + "," + ex.StackTrace);

            }
        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {

            if (e.Error == null)
            {



                //透過 GetResourceStream 方法取得的其他套件,提供資源資料流資訊

                StreamResourceInfo xapResource = new StreamResourceInfo(e.Result, null);



                //將其中的AppManifest讀取出來

                StreamResourceInfo appManifest = Application.GetResourceStream(xapResource, new Uri("AppManifest.xaml", UriKind.Relative));



                //建立xml reader

                XmlReader 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 assembly = part.Load(dllStreamInfo.Stream);

                        list.Add(assembly);

                    }

                }



                Assembly appAssembly =

                    list.SingleOrDefault((a) =>

                    a.FullName.StartsWith("SilverlightApplicationLdaptest") //这个和XAP包 里面DLL名字必须相同
                    
                );





                Type type = appAssembly.GetType("SilverlightApplicationLdaptest.MainPage"); //呼叫MainPage那Contol



                UserControl control = Activator.CreateInstance(type) as UserControl;



                border1.Child = control;



            }

        }
    }
}


2、 你调用XAP 如果用到Web引用 还有App 级别的资源,那么你的主项目就是 调用方必须也要引用。否则会报找不到web服务节点或者找不到资源的问题。

3、如果你用到WebClient那么是无法在本机就是直接打开Html页面方式使用。必须使用服务器,就是地址是http://这种

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值