提供建構,用以在 <DeviceSpecific> 項目的多個內容替代選擇中指定一個選項。
命名空間: System.Web.UI.MobileControls
組件: System.Web.Mobile (在 system.web.mobile.dll 中)
語法
'宣告
Public Class DeviceSpecific
Inherits Control
'用途
Dim instance As DeviceSpecific
public class DeviceSpecific : Control
public ref class DeviceSpecific : public Control
public class DeviceSpecific extends Control
public class DeviceSpecific extends Control
備註
在 <DeviceSpecific> 項目中,您通常會指定一或多個 <Choice> 項目,每個項目都包含屬性 (Attribute),其針對目標裝置功能指定評估選項的方式。在執行階段,會依序評估每個選項,並且使用第一個通過評估的選項。DeviceSpecific/Choice 建構會用來指定樣板集和覆寫屬性;例如,此建構可用於指定 Image 控制項的裝置專屬影像。
注意事項 |
|---|
即使 DeviceSpecific 類別 (Class) 繼承自 Web Form System.Web.UI.Control 命名空間,它也只是實作 (Implementation) 詳細資料。<Choice> 項目的行為不同於控制項。 |
範例
在下列程式碼中,示範了如何使用 DeviceSpecific 和 DeviceSpecificChoice 物件在行動表單中建立各種裝置特定的介面。
注意事項 |
|---|
以下程式碼範例使用單一檔案程式碼模型,如果直接複製到程式碼後置 (Code-Behind) 檔案即可能無法作用。這個程式碼範例必須複製到副檔名為 .aspx 的空白文字檔。如需詳細資訊,請參閱 ASP.NET Web 網頁程式碼模型。 |
<%@ Page Language="VB"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script runat="server">
Private Sub Form_Init(ByVal sender As Object, ByVal e As EventArgs)
' Create a DeviceSpecific group for Choice elements
Dim devSpecific As New DeviceSpecific()
Dim ipa As IParserAccessor
' Create two Choice objects, one with a filter
For i As Integer = 0 To 1
Dim devChoice As DeviceSpecificChoice
Dim custTemplate As ITemplate
' Create a Choice object
devChoice = New DeviceSpecificChoice()
' Only the first Choice has a filter (must be in Web.config)
If i = 0 Then
devChoice.Filter = "isHTML32"
' Create the header template.
custTemplate = New CustomTemplate("HeaderTemplate")
' Put header template in a new container
custTemplate.InstantiateIn(New TemplateContainer())
' Add the header template to the Choice
devChoice.Templates.Add("HeaderTemplate", custTemplate)
' Create the footer template
custTemplate = New CustomTemplate("FooterTemplate")
' Put footer template in a new container
custTemplate.InstantiateIn(New TemplateContainer())
' Add the footer template to the Choice
devChoice.Templates.Add("FooterTemplate", custTemplate)
End If
' Add the Choice to the DeviceSpecific
ipa = CType(devSpecific, IParserAccessor)
ipa.AddParsedSubObject(devChoice)
Next
' Add the DeviceSpecific object to the form
ipa = CType(Form1, IParserAccessor)
ipa.AddParsedSubObject(devSpecific)
End Sub
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As EventArgs)
Dim lab As System.Web.UI.MobileControls.Label
lab = CType(Form1.Header.FindControl("Label1"), _
System.Web.UI.MobileControls.Label)
If IsNothing(lab) Then Return
' Get the selected choice's filter name
Dim filterName As String = _
Form1.DeviceSpecific.SelectedChoice.Filter
If filterName = "isHTML32" Then
lab.Text += " - HTML32"
Else
lab.Text += " - Default"
End If
End Sub
Public Class CustomTemplate
Implements ITemplate
Dim templName As String
' Constructor
Public Sub New(ByVal TemplateName As String)
templName = TemplateName
End Sub
Public Sub InstantiateIn(ByVal container As Control) _
Implements ITemplate.InstantiateIn
If templName = "HeaderTemplate" Then
' Create a label
Dim lab As New System.Web.UI.MobileControls.Label
lab.Text = "Header Template"
lab.ID = "Label1"
' Create a command
Dim cmd As New Command()
cmd.Text = "Submit"
' Add controls to the container
container.Controls.Add(lab)
container.Controls.Add(cmd)
ElseIf templName = "FooterTemplate" Then
' Create a label
Dim lab As System.Web.UI.MobileControls.Label
lab = New System.Web.UI.MobileControls.Label()
lab.ID = "Label2"
lab.Text = "Footer Template"
' Add label to the container
container.Controls.Add(lab)
End If
End Sub
End Class
</script>
<html xmlns="http:'www.w3.org/1999/xhtml" >
<body>
<mobile:form id="Form1" runat="server"
OnInit="Form_Init">
</mobile:form>
</body>
</html>
<%@ Page Language="C#"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script runat="server">
private void Form_Init(object sender, System.EventArgs e)
{
// Create a DeviceSpecific group for Choice elements
DeviceSpecific devSpecific = new DeviceSpecific();
// Create two Choice objects, one with a filter
for (int i = 0; i < 2; i++)
{
DeviceSpecificChoice devChoice;
ITemplate custTemplate;
// Create a Choice object
devChoice = new DeviceSpecificChoice();
// Only the first Choice has a filter (must be in Web.config)
if (i == 0)
devChoice.Filter = "isHTML32";
// Create the header template.
custTemplate = new CustomTemplate("HeaderTemplate");
// Put header template in a new container
custTemplate.InstantiateIn(new TemplateContainer());
// Add the header template to the Choice
devChoice.Templates.Add("HeaderTemplate", custTemplate);
// Create the footer template
custTemplate = new CustomTemplate("FooterTemplate");
// Put footer template in a new container
custTemplate.InstantiateIn(new TemplateContainer());
// Add the footer template to the Choice
devChoice.Templates.Add("FooterTemplate", custTemplate);
// Add the Choice to the DeviceSpecific
((IParserAccessor)devSpecific).AddParsedSubObject(devChoice);
}
// Add the DeviceSpecific object to the form
((IParserAccessor)Form1).AddParsedSubObject(devSpecific);
}
protected void Page_Load(object sender, EventArgs e)
{
System.Web.UI.MobileControls.Label lab;
lab = (System.Web.UI.MobileControls.Label)Form1.Header.FindControl("Label1");
if (lab == null)
return;
// Get the selected choice's filter name
string filterName =
Form1.DeviceSpecific.SelectedChoice.Filter;
if (filterName == "isHTML32")
lab.Text += " - HTML32";
else
lab.Text += " - Default";
}
public class CustomTemplate : ITemplate
{
String templateName;
// Constructor
public CustomTemplate(string TemplateName)
{
templateName = TemplateName;
}
public void InstantiateIn(Control container)
{
if (templateName == "HeaderTemplate")
{
// Create a label
System.Web.UI.MobileControls.Label lab;
lab = new System.Web.UI.MobileControls.Label();
lab.Text = "Header Template";
lab.ID = "Label1";
// Create a command
Command cmd = new Command();
cmd.Text = "Submit";
// Add controls to the container
container.Controls.Add(lab);
container.Controls.Add(cmd);
}
else if (templateName == "FooterTemplate")
{
// Create a label
System.Web.UI.MobileControls.Label lab;
lab = new System.Web.UI.MobileControls.Label();
lab.ID = "Label2";
lab.Text = "Footer Template";
// Add label to the container
container.Controls.Add(lab);
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:form id="Form1" runat="server" OnInit="Form_Init">
</mobile:form>
</body>
</html>
所有上述程式碼都可以使用下列標記以宣告方式取代:
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:Form id="Form1" runat="server">
<mobile:DeviceSpecific Runat="server">
<Choice Filter="isHTML32">
<HeaderTemplate>
<mobile:Label ID="Label1" Runat="server">
Header Template - HTML32</mobile:Label>
<mobile:Command Runat="server">
Submit</mobile:Command>
</HeaderTemplate>
<FooterTemplate>
<mobile:Label ID="Label2" Runat="server">
Footer Template</mobile:Label>
</FooterTemplate>
</Choice>
<Choice>
<HeaderTemplate>
<mobile:Label ID="Label1" Runat="server">
Header Template - Default</mobile:Label>
<mobile:Command ID="Command1" Runat="server">
Submit</mobile:Command>
</HeaderTemplate>
<FooterTemplate>
<mobile:Label ID="Label2" Runat="server">
Footer Template</mobile:Label>
</FooterTemplate>
</Choice>
</mobile:DeviceSpecific>
</mobile:Form>
</body>
</html>
在這些範例中,Web.config 檔必須含有下列項目:
<!-- In Web.config file -->
<deviceFilters>
<filter name="isHTML32" compare="PreferredRenderingType" argument="html32" />
</deviceFilters>
.NET Framework 安全性
- AspNetHostingPermission 用於裝載環境中的作業。要求值:LinkDemand,權限值:Minimal。
- AspNetHostingPermission 用於裝載環境中的作業。要求值:InheritanceDemand,權限值:Minimal。
繼承階層架構
System.Object
System.Web.UI.Control
System.Web.UI.MobileControls.DeviceSpecific
執行緒安全
這個型別的所有公用靜態成員 (即 Visual Basic 中的 Shared 成員) 都是安全執行緒。並非所有的執行個體成員均為安全執行緒。
平台
Windows 98、 Windows 2000 SP4、 Windows Millennium Edition、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition
.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。
版本資訊
.NET Framework
支援版本:2.0、1.1
請參閱
參考
DeviceSpecific 成員
System.Web.UI.MobileControls 命名空間
Choices
其他資源
<DeviceSpecific> 項目
<Choice> 項目 (.NET Framework 開發人員手冊)
DeviceSpecific 控制項簡介
注意事項