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;
        }

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

原文地址:https://www.cnblogs.com/kurt/p/3557715.html