← 返回所有博客文章

在发布 Silverlight 应用程序时,为了能够让应用程序在客户端有最佳的初始加载速度,我们会将初始画面中用不到的资源进行按需加载,当发生页面导航操作时,我们会根据新页面的需要加载更多的资源,本文主要演示如何在客户端按需加载 C1DataGrid 所需的资源文件。

实现,我们创建一个名为 C1Datagrid_LoadOnDemand_Silverlight 的 Silverlight 应用程序,以及所对应的 Web 应用程序。

创建完成之后再给解决方法添加一个名为 C1SilverlightControlsLibrary 的 Silverlight 类库项目,我们将 Silverlight 应用程序中需要的控件都添加到该项目中,并在该项目添加一个名为 C1DataGridPagerControl.xaml 的 Silverlight 用户控件。

1、创建 C1SilverlightControlsLibrary 工程

从 Visual Studio 工具箱中将 C1 Silverlight 5.0 分类下的 C1DataGrid 添加到 C1DataGridPagerControl 的 XAML 设计界面。然后,在 C1SilverlightControlsLibrary 项目文件上鼠标右键,选择添加引用菜单,并在出现的对话框中选择 C1.Silverlight.DataGrid.Ria.5,引用添加完成之后在 C1DataGridPagerControl  的 XAML 代码视图中添加 C1DataPager 控件,最终的 XAML 代码如下:

<UserControl x:Class="C1SilverlightControlsLibrary.C1DataGridPagerControl"
    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:DesignHeight="350" d:DesignWidth="400" xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml">
    
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <c1:C1DataGrid Margin="10,10,12,0" Name="c1DataGrid1" VerticalAlignment="Top" Height="300" />
        <c1:C1DataPager Source="{Binding ItemsSource, ElementName=c1DataGrid1}" HorizontalAlignment="Right" Name="c1DataPager1" VerticalAlignment="Top" PageSize="10" Grid.Row="1" />
    </Grid>
</UserControl>

 

2、将 C1SilverlightControlsLibrary 和相关 DLL 拷贝到 ClientBin 目录下

编译 C1SilverlightControlsLibrary 工程,并将 编译之后 bin 目录下的以下文件拷贝到 C1Datagrid_LoadOnDemand_Silverlight.Web 项目的 ClientBin 目录中:

1 C1.Silverlight.5.dll
2 C1.Silverlight.DataGrid.5.dll
3 C1.Silverlight.DataGrid.Ria.5.dll
4 C1.Silverlight.DateTimeEditors.5.dll
5 C1.Silverlight.Extended.5.dll
6 System.ComponentModel.DataAnnotations.dll
7 System.Windows.Controls.dll
8 System.Windows.Data.dll
9 C1SilverlightControlsLibrary.dll
10 zh-Hans/C1.Silverlight.5.resources.dll
11 zh-Hans/C1.Silverlight.DataGrid.5.resources.dll
12 zh-Hans/C1.Silverlight.DataGrid.Ria.5.resources.dll
13 zh-Hans/C1.Silverlight.Extended.5.resources.dll

 

3、在 C1Datagrid_LoadOnDemand_Silverlight 中按需加载 C1SilverlightControlsLibrary.dll 和相关DLL。

在 MainPage 后台代码中添加以下代码,以完成按需加载操作:

 

    public partial class MainPage : UserControl
    {
        // 记录需要下载的资源数量
        private int dllLoadCount = 0;
        // 资源下载对象
        private WebClient dllDownloader = new WebClient();
        public MainPage()
        {
            InitializeComponent();
            dllDownloader.OpenReadCompleted +=
                new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            // 下载主 DLL
            dllDownloader.OpenReadAsync(
                new Uri("C1SilverlightControlsLibrary.dll", UriKind.Relative));
        }
        private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if ((e.Error == null) && (e.Cancelled == false))
            {
                AssemblyPart assemblyPart = new AssemblyPart();
                assemblyPart.Load(e.Result);
                dllLoadCount++;
                switch (dllLoadCount)
                {
                    case 1:
                        dllDownloader.OpenReadAsync(
                            new Uri("C1.Silverlight.5.dll", UriKind.Relative));
                        break;
                    case 2:
                        dllDownloader.OpenReadAsync(
                            new Uri("C1.Silverlight.DataGrid.5.dll", UriKind.Relative));
                        break;
                    case 3:
                        dllDownloader.OpenReadAsync(
                            new Uri("C1.Silverlight.DataGrid.Ria.5.dll", UriKind.Relative));
                        break;
                    case 4:
                        dllDownloader.OpenReadAsync(
                            new Uri("C1.Silverlight.DateTimeEditors.5.dll", UriKind.Relative));
                        break;
                    case 5:
                        dllDownloader.OpenReadAsync(
                            new Uri("C1.Silverlight.Extended.5.dll", UriKind.Relative));
                        break;
                    case 6:
                        dllDownloader.OpenReadAsync(
                            new Uri("zh-Hans/C1.Silverlight.5.resources.dll", UriKind.Relative));
                        break;
                    case 7:
                        dllDownloader.OpenReadAsync(
                            new Uri("zh-Hans/C1.Silverlight.DataGrid.5.resources.dll", UriKind.Relative));
                        break;
                    case 8:
                        dllDownloader.OpenReadAsync(
                            new Uri("zh-Hans/C1.Silverlight.DataGrid.Ria.5.resources.dll", UriKind.Relative));
                        break;
                    case 9:
                        dllDownloader.OpenReadAsync(
                            new Uri("System.Windows.Data.dll", UriKind.Relative));
                        break;
                    case 10:
                        dllDownloader.OpenReadAsync(
                            new Uri("System.ComponentModel.DataAnnotations.dll", UriKind.Relative));
                        break;
                    case 11:
                        dllDownloader.OpenReadAsync(
                            new Uri("System.Windows.Controls.dll", UriKind.Relative));
                        break;
                    case 12:
                        // 所需 DLL 下载完成之后,通过反射创建 C1DataGridPagerControl 实例
                        Control control = Activator.CreateInstance(Type.GetType("C1SilverlightControlsLibrary.C1DataGridPagerControl, C1SilverlightControlsLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")) as Control;
                        this.LayoutRoot.Children.Add(control);
                        break;
                    default:
                        break;
                }
            }
        }
    }

 

运行截图:

image

 

源码下载:VS2010 + C1 Studio for Silverlight 2013V1