Silverlight:利用异步加载Xap实现自定义loading效果
2009-12-22 17:28
726 查看
关键点:
1.利用WebClient的DownloadProgressChanged事件更新下载进度
2.下载完成后,分析Xap包的程序集Assembly信息
3.利用Assembly反射还原对象并加载到当前页中。
好处:
1.可以先定义一个简单的加载动画,吸引用户注意,避免长时间的无聊等待,改善用户体验。
2.实现按需加载,避免一次性下载过多内容。
3.在一定程度上,增加了破解难度,有助于代码保密。
Xaml :
代码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/
1.利用WebClient的DownloadProgressChanged事件更新下载进度
2.下载完成后,分析Xap包的程序集Assembly信息
3.利用Assembly反射还原对象并加载到当前页中。
好处:
1.可以先定义一个简单的加载动画,吸引用户注意,避免长时间的无聊等待,改善用户体验。
2.实现按需加载,避免一次性下载过多内容。
3.在一定程度上,增加了破解难度,有助于代码保密。
Xaml :
代码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/
相关文章推荐
- 利用canvas加vauleAnimator自定义drawable实现加载loading效果
- Android自定义View实现loading动画加载效果
- .net MVC框架下利用CSS+js实现loading效果
- Android 三种方式实现自定义圆形页面加载中效果的进度条
- 【iOS开发-24】导航控制器下不同视图控制器之间切换:利用CATrasition和view的layer层来实现自定义的动画效果
- Android开发之图片处理专题(二):利用AsyncTask和回调接口实现图片的异步加载和压缩
- 使用 CSS3 实现超炫的 Loading(加载)动画效果
- JAVAWEB开发之Servlet3.0新特性的使用以及注解的详细使用和自定义注解的方法、动态代理的使用、利用动态代理实现细粒度的权限控制以及类加载和泛型反射
- Android自定义Dialog实现文字动态加载效果
- 详解RecyclerView+BGARefreshLayout实现自定义下拉刷新、上拉加载和侧滑删除效果
- 利用PreLoader实现一个平视显示(HUD)效果(可以运用到加载等待效果),并进行简单的讲解
- 利用jQuery的deferred对象实现异步按顺序加载JS文件
- 【原】改进了的"利用Silverlight实现类似iGoogle的浮动拖拽效果"
- Windows Phone 利用Async CTP实现异步加载数据
- 利用SoftReference实现图片异步加载
- Android-自定义TextView和异步加载图片的实现
- CSS实现loading加载效果
- Android中自定义ProgressDialog实现加载滚动条(加载中…)效果
- 利用 Silverlight 实现类似 iGoogle 的浮动层拖拽效果
- 利用DiskLruCache 实现异步加载图片