动态加载XAML文件(2)

本文介绍了如何使用C#代码在Silverlight应用中通过WebClient类下载并加载远程XAP文件,包括下载进度更新和资源注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值