惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security Blog

博客园 - 愛如風過

MODRD 指令 读取地址是哪儿来的 如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)? easyui datagrid 的分页刷新按钮 HttpRequest Get Post,WebClient Get GetWindowThreadProcessId用法(转) EASYUI DATAGRID 多列复选框CheckBox Android Service 数据库主键设计之思考[转] Silverlight 使用用户控件 [转] - 愛如風過 Jquery 实现“最近浏览过的商品”的功能 - 愛如風過 - 博客园 Jquery获取设置radio select checkbox 文本框 [转] - 愛如風過 泛型 Vista 中,使用VS 2005调试程序没有权限的问题? Team Foundation 和 Visual SourceSafe 之间的区别 MSF资源 网页左右浮动广告(包括ASP.NET后台管理) why? 在水晶报表中实现任意选择指定字段显示资料 .在Datagrid中修改数据,当点击编辑键时,数据出现在文本框中,怎么控制文本框的大小 ?(转)
技巧:在Silverlight 2应用程序中切换用户控件[转]
愛如風過 · 2008-12-24 · via 博客园 - 愛如風過

摘要

大家都知道,在Silverlight 2应用程序中,每个应用程序将生成一个xap文件,每一个xap文件中只能设置一个起始的用户控件。如果我们有多个用户控件,需要在不同的ASP.NET页面中加载,最简单的方法莫过于针对多个用户控件分别建立对应的Silverlight项目,但这种方式有很多的缺点,如我们的样式文件需要在多个项目中进行拷贝。

本文将介绍利用初始化参数进行用户控件的切换这一技巧。

准备

现在建立一个项目结构如下图所示,在Silverlight项目中我们有个三个用户控件:ContentPage、DefaultPage、MasterPage,需要在不同的ASP.NET页面加载时显示不同的用户控件。

TerryLee_0099

思路

要实现这个功能并不是什么难事,我们完全可以使用InitParams这个属性,如下图所示:

TerryLee_0100

或者在HTML中通过param指定InitParameters:

TerryLee_0101

该属性是一个Dictionary<string,string>类型的,我们可以在其中设置一系列的键-值对初始化参数,用逗号“,”分割开。所以我们的思路非常简单,就是在ASP.NET页面或者HTML中通过InitParameters指定起始用户控件,然后在Application_Startup事件中获取参数,并设置RootVisual。

实现

如下面这段代码,我们设置一个初始化参数InitPage为ContentPage:

<asp:Silverlight ID="Xaml1" runat="server"
     Source="~/ClientBin/SwitchUserControl.xap"
     MinimumVersion="2.0.30523"
     Width="100%" Height="100%"
     InitParameters="InitPage=ContentPage"/>

然后在Application_Startup根据参数不同设置不同的RootVisual:

private void Application_Startup(object sender, StartupEventArgs e)
{
    if (!e.InitParams.ContainsKey("InitPage"))
    {
        this.RootVisual = new DefaultPage();
        return;
    }
    switch (e.InitParams["InitPage"])
    {
        case "MasterPage":
            this.RootVisual = new MasterPage();
            break;
        case "ContentPage":
            this.RootVisual = new ContentPage();
            break;
        default:
            this.RootVisual = new DefaultPage();
            break;
    }
}

现在运行程序后,可以看到起始用户控件为ContentPage,如下图所示:

TerryLee_0102 

改进

上面这种方式虽然达到了我们的目的,但是switch语句代码实在不怎么优雅,如果有几十个用户控件,那就得有几十个分支。既然我们在初始话参数中设置了起始用户控件名,为什么不直接使用反射呢?在Silverlight 2中,对于反射提供了很好的支持,所以我们的代码可以修改如下:

private void Application_Startup(object sender, StartupEventArgs e)
{
    if (!e.InitParams.ContainsKey("InitPage"))
    {
        this.RootVisual = new DefaultPage();
        return;
    }

    Assembly assembly = Assembly.GetExecutingAssembly();
    String rootName = String.Format("SwitchUserControl.{0}", e.InitParams["InitPage"]);
    UIElement rootVisual = assembly.CreateInstance(rootName) as UIElement;
    this.RootVisual = rootVisual;
}

现在代码看起来好多了,即便有再多的用户控件也不用再修改此处的代码。但是大家一定要注意一个问题,就是要合理的划分Silverlight项目,不要把所有的用户控件都放在一个项目里面,避免xap文件过大。在后面我还会写一篇文章来谈谈如何合理的划分Silverlight项目结构,以及如何调用其它xap文件中的用户控件。