MobileCapabilities类能为不同的设备显示不同的内容。通过一个名为<DeviceSpecific>的特殊元素,可以使用MobileCapabilities类的属性。
粗略的说,<DeviceSpecifc>元素的工作机制类似于Visual Basic的Select Case语句。<DeviceSpecific>可以包含多个<Choice>元素,用于匹配不同的设备标准。在<Choice>元素匹配时,就返回其内容。
在程序清单中,页面针对HTML和WML的设备显示不同的内容。如果设备时HTML兼容的,在List控件的头部和尾部就应用特殊的格式。此外,每个列表项是在List控件的ItemTemplate中格式化的。
当程序在HTML3.2兼容的浏览器中时,就显示HeaderTemplate和FooterTemplate中的特殊格式。当在WML兼容的浏览器中显示时,所有的模板将被忽略。List控件以其默认的方式显示。
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="C#" %>
<script language="C#" runat=server>
void Page_Load(object sender, EventArgs e)
{
ArrayList colMovies = new ArrayList();
colMovies.Add( "Star Wars" );
colMovies.Add( "Gone with the Wind" );
colMovies.Add( "Citizen Kane" );
lstMovies.DataSource = colMovies;
lstMovies.DataBind();
}
void List_ItemCommand( object s, ListCommandEventArgs e ) {
lblMovie.Text = e.ListItem.Text;
ActiveForm = form2 ;
}
</Script>
<Mobile:Form runat="server" id="form1">
<Mobile:List
ID="lstMovies"
OnItemCommand="List_ItemCommand"
Runat="Server">
<DeviceSpecific>
<Choice Filter="IsHTML32">
<HeaderTemplate>
<h2>Welcome to the Movie Reservation System!</h2>
</HeaderTemplate>
<ItemTemplate>
<p>
<asp:Button
Text='<%# Container.ToString() %>'
Runat="Server" />
</ItemTemplate>
<FooterTemplate>
<p>
<small>All Contents © copyright 2002 by the Company</small>
</FooterTemplate>
</Choice>
</DeviceSpecific>
</Mobile:List>
</Mobile:Form>
<Mobile:Form runat="server" id="form2">
<b>You selected:</b>
<br/>
<Mobile:Label ID="lblMovie"
Runat="Server"/>
</Mobile:Form>