zoukankan      html  css  js  c++  java
  • Silverlight中以客户端加载另一项目客户端(先登录后加载的一般实现)

    近几日,本人在对一个老的Silverlight的GIS项目进行维护,发现该项目的实现是先把所有的资源加载至客户端,其中包括登录部分和业务实现部分,我就在想可不可以把登录部分和业务实现部分开来。如果用户输入的账号密码正确才开始加载业务实现方面的资源(1.由于Silverlight的运行资源的加载是一次性的。2.现在主流的实现都是如此)

    如下是我在代码中的实现

    private const string odll = "SilverlightApplication2.dll";
            private const string LoaderPageName = "SilverlightApplication2.MainPage";
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                const string name = "SilverlightApplication2.xap";
                var uri = new Uri(name, UriKind.Relative);
                WebClient webClient=new WebClient();
                if (webClient.IsBusy)
                {
                    webClient.CancelAsync();
                }
                webClient.OpenReadCompleted -= new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
                webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
                webClient.DownloadProgressChanged -= new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
                webClient.OpenReadAsync(uri);
            }
    
            void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
                //throw new NotImplementedException();
                this.textBlock1.Text = "已经加载 " + e.ProgressPercentage;
                if (e.ProgressPercentage == 100)
                {
                    return;
                }
            }
    
            void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
            {
                //throw new NotImplementedException();
                try
                {
                    Stream streamLoadXap = e.Result;
    
                    if (streamLoadXap != null)
                    {
                        Assembly assemblyDll = LoadAssemblyFromXap(streamLoadXap, odll);
    
                        if (assemblyDll != null)
                        {
                            UIElement elementXapMain = assemblyDll.CreateInstance(LoaderPageName) as UIElement;
    
                            if (elementXapMain != null)
                            {
                                this.Content = elementXapMain;
                            }
                            else
                            {
                                MessageBox.Show("惨了,程序加载失败了!!!");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
    
            /// <summary>  
            /// 从程序中获取程序集  
            /// </summary>  
            /// <param name="streamLoadXap"></param>  
            /// <param name="strAssemblyName"></param>  
            /// <returns></returns>  
            public Assembly LoadAssemblyFromXap(Stream streamLoadXap, string strAssemblyName)
            {
                try
                {
                    Stream streamXml = Application.GetResourceStream(new StreamResourceInfo(streamLoadXap, null), new Uri("AppManifest.xaml", UriKind.Relative)).Stream;
                    String appManifestString = new StreamReader(streamXml).ReadToEnd();
    
                    XElement deploymentRoot = XDocument.Parse(appManifestString).Root;  //创建XML文件,并增加加载的程序集内容新内容
                    if (deploymentRoot != null)
                    {
                        List<XElement> deploymentParts = (from assemblyParts in deploymentRoot.Elements().Elements() select assemblyParts).ToList();
                        Assembly asm = null;
    
                        foreach (var xElement in deploymentParts)
                        {
                            XAttribute xAttribute = xElement.Attribute("Source");
                            if (xAttribute != null)
                            {
                                string source = xAttribute.Value;
                                AssemblyPart assemblyPart = new AssemblyPart {Source = source};
    
                                StreamResourceInfo sri = Application.GetResourceStream(new StreamResourceInfo(streamLoadXap, "application/binary"), new Uri(source, UriKind.Relative));
    
                                if (source.Equals(odll))
                                {
                                    asm = assemblyPart.Load(sri.Stream);
                                }
                                else
                                {
                                    assemblyPart.Load(sri.Stream);
                                }
                            }
                        }
    
                        return asm;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return null;
                }
                return null;
            }

    当然还有其他功能可以实现,比如地址的跳转等。

  • 相关阅读:
    注解实现SpringCache自定义失效时间(升级版)
    表白小游戏之——制作一个小游戏给喜欢的人(Cocos Creator入门小案例)
    3.python编程与计算机的关系,如何执行python文件
    如何临时发布部署Cocos小游戏到Linux服务器,让别人能在微信打开
    当互联网公司换上东京奥运会图标
    灵魂画手的零基础python教程1:关于Python学习的误区、python的优缺点、前景
    聊一聊关于聊天记录的存储
    【爬虫系列】1. 无事,Python验证码识别入门
    【爬虫系列】0. 无内鬼,破解前端JS参数签名
    JPA
  • 原文地址:https://www.cnblogs.com/kurt/p/3557715.html
Copyright © 2011-2022 走看看