|
using System;
using System.Net;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Reflection;
using System.Windows.Resources;
namespace XapLoader
{
public partial class Splash : UserControl
{
private int downloadProgress;
public Splash()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Splash_Loaded);
this.SizeChanged += new SizeChangedEventHandler((Object sender,
SizeChangedEventArgs e)
=> { UpdateProgressBar(); });
}
private void Splash_Loaded(object sender, RoutedEventArgs e)
{
string contentUri = Application.Current.Host.Source.AbsoluteUri.Replace(
"XapLoader", "XapContent");
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.DownloadProgressChanged
+= new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.OpenReadAsync(new Uri(contentUri));
}
private void wc_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
{
Assembly assembly = LoadAssemblyFromXap("XapContent.dll", e.Result);
object loadedContent = assembly.CreateInstance("XapContent.Main");
App app = (App)Application.Current;
app.ApplicationVisualRoot.Children.Clear();
app.ApplicationVisualRoot.Children.Add((UserControl)loadedContent);
}
private void wc_DownloadProgressChanged(Object sender, DownloadProgressChangedEventArgs e)
{
downloadProgress = e.ProgressPercentage;
UpdateProgressBar();
}
private void UpdateProgressBar()
{
rectProgress.Width = rectStandard.ActualWidth * downloadProgress / 100;
txtProgress.Text = "Loading ... " + downloadProgress.ToString() + "%";
}
private Assembly LoadAssemblyFromXap(string relativeUri, Stream xapPackageStream)
{
StreamResourceInfo xapPackageSri = new StreamResourceInfo(xapPackageStream, null);
StreamResourceInfo assemblySri = Application.GetResourceStream(xapPackageSri,
new Uri(relativeUri, UriKind.Relative));
AssemblyPart assemblyPart = new AssemblyPart();
return assemblyPart.Load(assemblySri.Stream);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.