Silverlight: Use asynchronous loading of xap for custom Loading

Source: Internet
Author: User
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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.