每个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();
}
}

7747

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



