Silverlight:利用非同步載入Xap實現自訂loading效果

來源:互聯網
上載者:User
關鍵點:
1.利用WebClient的DownloadProgressChanged事件更新下載進度
2.下載完成後,分析Xap包的程式集Assembly資訊

3.利用Assembly反射還原對象並載入到當前頁中。

 

好處:
1.可以先定義一個簡單的載入動畫,吸引使用者注意,避免長時間的無聊等待,改善使用者體驗。
2.實現按需載入,避免一次性下載過多內容。

3.在一定程度上,增加了破解難度,有助於代碼保密。

Xaml :

代碼<UserControl x:Class="LoadXap.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:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
      <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
        <ProgressBar Height="15" VerticalAlignment="Center" HorizontalAlignment="Center" Width="200" x:Name="pb1" Value="0"/>
        <TextBlock x:Name="txtLoad" Text="0%" Margin="5,0,0,0"></TextBlock>
    </StackPanel>       
  </Grid>
</UserControl>

CS代碼:

代碼using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Resources;
using System.Xml;

namespace LoadXap
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            WebClient wc = new WebClient();
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
            Uri xapUri = new Uri(HtmlPage.Document.DocumentUri, "ClientBin/MainXap.xap");
            wc.OpenReadAsync(xapUri);
        }

        void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.txtLoad.Text = e.ProgressPercentage.ToString() + "%";
            this.pb1.Value = (double)e.ProgressPercentage;
        }

        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            Assembly assembly = GetAssemblyFromXap(e.Result, "MainXap.dll");
            UIElement element = assembly.CreateInstance("MainXap.MainPage") as UIElement;
            this.LayoutRoot.Children.Add(element);
        }

        /// <summary>
        /// 從XAP包中返回程式集資訊
        /// </summary>
        /// <param name="packageStream"></param>
        /// <param name="assemblyName"></param>
        /// <returns></returns>
        private Assembly GetAssemblyFromXap(Stream packageStream, String assemblyName)
        {
            Stream stream = Application.GetResourceStream(new StreamResourceInfo(packageStream, null), new Uri("AppManifest.xaml", UriKind.Relative)).Stream;
            Assembly asm = null;
            XmlReader xmlReader = XmlReader.Create(stream);
            xmlReader.MoveToContent();
            if (xmlReader.ReadToFollowing("Deployment.Parts"))
            {
                string str = xmlReader.ReadInnerXml();
                Regex reg = new Regex("x:Name=\"(.+?)\"");
                Match match = reg.Match(str);
                string sName = "";
                if (match.Groups.Count == 2)
                {
                    sName = match.Groups[1].Value;
                }
                reg = new Regex("Source=\"(.+?)\"");
                match = reg.Match(str);
                string sSource = "";
                if (match.Groups.Count == 2)
                {
                    sSource = match.Groups[1].Value;
                }
                AssemblyPart assemblyPart = new AssemblyPart();
                StreamResourceInfo streamInfo = App.GetResourceStream(new StreamResourceInfo(packageStream, "application/binary"), new Uri(sSource, UriKind.Relative));
                if (sSource == assemblyName)
                {
                    asm = assemblyPart.Load(streamInfo.Stream);
                }
            }
            return asm;
        }
    }
}

示範效果:http://images.24city.com/jimmy/loadXap/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.