C#代码部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
namespace SilverlightApplication1
{
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.DownloadProgressChanged += new DownloadProgressChangedEventHandler(_wc_DownloadProgressChanged);
_wc.OpenReadCompleted += new OpenReadCompletedEventHandler(_wc_OpenReadCompleted);
_wc.OpenReadAsync(new Uri("SilverlightApplication2.xap", UriKind.Relative));
}
void _wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
_progress1.Text = e.ProgressPercentage.ToString() + "%";
if (e.ProgressPercentage == 100)
{
_progress1.Visibility = Visibility.Collapsed;
}
}
void _wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
System.Windows.Resources.StreamResourceInfo Info = new System.Windows.Resources.StreamResourceInfo(e.Result, "application/binary");
AssemblyPart AP = new AssemblyPart();
System.Reflection.Assembly Asm;
System.Windows.Resources.StreamResourceInfo streamInfo = Application.GetResourceStream(Info, new Uri("SilverlightApplication2.dll", UriKind.Relative));
Asm = AP.Load(streamInfo.Stream);
var obj = (Asm.CreateInstance("SilverlightApplication2.MainPage") as FrameworkElement);
obj.SetValue(Grid.ColumnProperty, 0);
obj.SetValue(Grid.RowProperty, 0);
LayoutRoot.Children.Add(obj);
}
}
}
XAML部分:
<UserControl x:Class="SilverlightApplication1.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">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="_progress1" FontSize="30" Text="0%" Grid.Row="0" Grid.Column="0"/>
<TextBlock x:Name="_progress2" FontSize="30" Text="0%" Grid.Row="0" Grid.Column="1"/>
<TextBlock x:Name="_progress3" FontSize="30" Text="0%" Grid.Row="1" Grid.Column="0"/>
<TextBlock x:Name="_progress4" FontSize="30" Text="0%" Grid.Row="1" Grid.Column="1"/>
</Grid>
</UserControl>
本文介绍了如何使用C#代码在Silverlight应用中通过WebClient类下载并加载远程XAP文件,包括下载进度更新和资源注入。
845

被折叠的 条评论
为什么被折叠?



