动态设置Silverlight 初始化参数以及设置控件中设置默认.xaml 页面


每个Silverlight 项目可以有多个.xaml 文件。但每次只能看到一个.xaml 页面。
当你在Visual Studio 中创建一个Silverlight 项目是,将创建一个默认的名字叫做
“MainPage.xaml”的.xaml 文件,当打开承载Silverlight 项目的Web 页面时这个.xaml
控件默认显示。
可以通过设置“Application.RootVisual”属性来更改默认的.xaml 页面。

打开Silverlight 项目中的App.xaml.cs 文件。看看Application_Startup
事件处理程序。在这里可以看见放置的默认.xaml 页面。为了改变默认的.xaml 页面,可
以设置Application.RootVisual 属性指定在.xaml 页面使用的类名名称。

View Code
    
    
private void Application_Startup( object sender, StartupEventArgs e) { this .RootVisual = new MainPage(); }

你可以改变上面代码中的类名,默认打开其他xaml 文件

在Silverlight 应用程序中,可以有多个XAML 页面。当在Web 页面中使用.xap 文件时,
一次仅有一个XAML 文件显示。在App.xaml 文件中通过以上的代码可以决定默认显示的
XAML 文件。

例如,你可能考虑有一个xaml
文件提供用户输入登录信息。当用户点击“提交”按钮后,在登录验证成功后,可能想重定
向到其他的xaml 文件。
这时可以设置xaml 页面的“Content”属性。

View Code
    
    
private void SubmitButton_Click( object sender, RoutedEventArgs e) { this .Content = new NewXamlPage(); }

上面代码显示如何在按钮的单击事件处理中设置xaml 控件的“Content”属性。当用户
点击提交按钮后,新的xaml 文件(NewXamlPage)将打开,原始的xaml 文件将回收。

可能有这种情况,从不同的Web 页面指定相同的.xap 文件显示不同的XAML 文件。可以通
过xaml 文件名或通过Silverlight 控件的InitParameters 属性确定一些其他类型。
这个属性可以从Web 页面设置Silverlight 控件。从Web 页面设置的值将通过
Silverlight 控件读取并打开合适的xaml 页面。(asp.net页面调用合适的SL控件)

方法有2种:

1、采用object

View Code
复制代码
    
    
< form id = " form1 " runat = " server " style = " height:100%; " > < div id = " silverlightControlHost " > < object data = " data:application/x-silverlight-2, " type = " application/x-silverlight-2 " width = " 100% " height = " 100% " > < param name = " source " value = " ClientBin/SilverlightWeb.xap " /> < param name = " InitParams " value = " InitPage=Player " /> < param name = " onerror " value = " onSilverlightError " /> < param name = " background " value = " white " /> < param name = " minRuntimeVersion " value = " 3.0.40624.0 " /> < param name = " autoUpgrade " value = " true " /> < a href = " http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0 " style = " text-decoration: none; " > < img src = " http://go.microsoft.com/fwlink/?LinkId=108181 " alt = " 获取 Microsoft Silverlight " style = " border-style: none " /> </ a > </ object >< iframe id = " _sl_historyFrame " style = ' visibility:hidden;height:0;width:0;border:0px ' ></ iframe ></ div > </ form >
复制代码

2、引用System.Web.Silverlight.dll控件

在页面里

View Code
复制代码
    
    
<% @ Register Assembly = " System.Web.Silverlight " Namespace = " System.Web.UI.SilverlightControls " TagPrefix = " asp " %> <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " > < html xmlns = " http://www.w3.org/1999/xhtml " > < head runat = " server " > < title ></ title > </ head > < body > < form id = " form1 " runat = " server " style = " height:100%; " > < asp:ScriptManager ID = " ScriptManager1 " runat = " server " > </ asp:ScriptManager > < asp:Silverlight InitParameters = " InitPage=Player " ID = " MainContainer " Source = " ~/ClientBin/SilverlightWeb.xap " runat = " server " MinimumVersion = " 2.0 " PluginBackground = " White " AutoUpgrade = " true " Width = " 100% " Height = " 500px " ></ asp:Silverlight > </ form > </ body > </ html >
复制代码

在上面代码,看见如下代码
<param name="InitParams" value="InitPage=Player" />

当有多个键值对时,使用如下格式(用逗号隔开)

<param name="InitParams" value="UserId=admin InitPage=page1,UploadUri=www/>

这里设置的“InitParameters”具有键-值对关系标识默认打开的xaml 页面。
现在,将在App.xaml 文件中读取这个属性并为Application.RootVisual 属性设置适
当的页面。这里有个示例用在App.xaml.cs 中实现这一目标:

View Code
复制代码
    
    
private void Application_Startup( object sender, StartupEventArgs e) { IDictionary < string , string > parameters = e.InitParams; if (parameters == null ) { // 没有参数传递时,打开默认的xaml this .RootVisual = new DefaultPage(); } else if (parameters[ " InitPage " ] == " Player " ) { this .RootVisual = new Page1(); } else if (parameters[ " PageName " ] == " Page2 " ) { this .RootVisual = new Page2(); } else { // 打开默认的xaml this .RootVisual = new DefaultPage(); } }
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值