Key points:
1. Update the download progress using the downloadprogresschanged event of WebClient
2. Analyze the xap package's Program Set assembly information
3. Restore the object using assembly reflection and load it to the current page.
Benefits:
1. You can first define a simple loading animation to attract users' attention, so as to avoid long delays and improve the user experience.
2. implement on-demand loading to avoid downloading too much content at a time.
3. To some extent, it increases the difficulty of cracking and helpsCodeConfidentiality.
XAML:
Code < 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" > </ Textblock >
</ Stackpanel >
</ Grid >
</ Usercontrol >
CS code:
Code 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
{< br> Public partial class mainpage: usercontrol
{< br> Public mainpage ()
{< br> initializecomponent ();
This. Loaded+ = NewRoutedeventhandler (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 = Getassemblyfromxap (E. result, " Mainxap. dll " );
Uielement = Assembly. createinstance ( " Mainxap. mainpage " ) As Uielement;
This . Layoutroot. Children. Add (element );
}
/// <Summary>
/// Returns assembly information from the xap package
/// </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. 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 = 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;
}
}
}
Demo: http://images.24city.com/jimmy/loadXap/