快速入門:新增頁面控制項 (HTML)
[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]
了解如何建立和顯示 PageControl 物件。
先決條件
我們假設您可以利用 JavaScript,以適用於 JavaScript 的 Windows Library 控制項建立基本的 Windows 市集應用程式。如需開始使用 WinJS 控制項的指示,請參閱快速入門:新增 WinJS 控制項與樣式。
建立 PageControl
與其他適用於 JavaScript 的 Windows Library 不同的是,您不會直接具現化 PageControl。相反地,您會呼叫 WinJS.UI.Pages.define 方法,並將用於定義 PageControl HTML 檔案的統一資源識別元 (URI) 以及用於定義 PageControl 成員的物件傳遞給它,進而建立 PageControl。
以下是 PageControl 定義的範例。它是由三個檔案組成:HTML 檔案、CSS 檔案及 JavaScript 檔案。
<!-- samplePageControl.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>samplePageControl</title>
<!-- WinJS references -->
<link href="/pages/samplePageControl.css" rel="stylesheet">
<script src="/pages/samplePageControl.js"></script>
</head>
<body>
<div class="samplePageControl">
<p class="samplePageControl-text"><span data-win-bind="textContent: controlText">Message goes here</span>
<button class="samplePageControl-button">Click me</button></p>
<p>Page controls can also contain WinJS controls. They are activated automatically.</p>
<div class="samplePageControl-toggle" data-win-control="WinJS.UI.ToggleSwitch"></div>
</div>
</body>
</html>
/* samplePageControl.css */
.samplePageControl
{
padding: 5px;
border: 4px dashed #999999;
}
// samplePageControl.js
(function () {
"use strict";
var ControlConstructor = WinJS.UI.Pages.define("/pages/samplePageControl.html", {
// This function is called after the page control contents
// have been loaded, controls have been activated, and
// the resulting elements have been parented to the DOM.
ready: function (element, options) {
options = options || {};
this._data = WinJS.Binding.as({ controlText: options.controlText, message: options.message });
// Data bind to the child tree to set the control text
WinJS.Binding.processAll(element, this._data);
// Hook up the click handler on our button
WinJS.Utilities.query("button", element).listen("click",
// JavaScript gotcha - use function.bind to make sure the this reference
// inside the event callback points to the control object, not to
// window
this._onclick.bind(this));
// WinJS controls can be manipulated via code in the page control too
WinJS.Utilities.query(".samplePageControl-toggle", element).listen("change",
this._ontoggle.bind(this));
},
// Getter/setter for the controlText property.
controlText: {
get: function () { return this._data.controlText; },
set: function (value) { this._data.controlText = value; }
},
// Event handler that was wired up in the ready method
_onclick: function (evt) {
WinJS.log && WinJS.log(this._data.message + " button was clicked", "sample", "status");
},
// Event handler for when the toggle control switches
_ontoggle: function (evt) {
var toggleControl = evt.target.winControl;
WinJS.log && WinJS.log(this._data.message + " toggle is now " + toggleControl.checked, "sample", "status");
}
});
// The following lines expose this control constructor as a global.
// This lets you use the control as a declarative control inside the
// data-win-control attribute.
WinJS.Namespace.define("Controls_PageControls", {
SamplePageControl: ControlConstructor
});
})();
如果要在 Microsoft Visual Studio 中建立頁面控制項,請從主功能表選取 [專案] > [加入新項目],然後選取 [頁面控制項]****。
顯示 PageControl
定義 PageControl 之後,您可以使用數種方法顯示它:
使用 WinJS.UI.Pages.render 函式。
<div class="renderingPageControls-renderedControl"></div>
// Render the page control via a call to WinJS.UI.Pages.render. This lets // you render a page control by referencing it via a url. var renderHost = element.querySelector(".renderingPageControls-renderedControl"); WinJS.UI.Pages.render("/pages/SamplePageControl.html", renderHost, { controlText: "This control created by calling WinJS.UI.Pages.render", message: "Render control" }).done();
公開 PageControl 物件的建構函式,然後用它來建立 PageControl。
<div class="renderingPageControls-createdProgrammatically"></div>
// Render the page control by creating the control. var constructedHost = element.querySelector(".renderingPageControls-createdProgrammatically"); new Controls_PageControls.SamplePageControl(constructedHost, { controlText: "This control created by calling the constructor directly", message: "Constructed control" });
使用 WinJS.UI.Pages.get 函式取得 PageControl 的建構函式。
具現化 HTML 中的控制項,就像適用於 Javascript 的 Windows Library 控制項一樣 (它的確是)。您必須公開 PageControl 物件的建構函式才能進行這個動作。
<div data-win-control="Controls_PageControls.SamplePageControl" data-win-options="{controlText: 'This was created declaratively', message: 'Declarative control' }"> </div>
使用 HtmlControl 轉譯頁面。
<div class="renderingPageControls-htmlControl" data-win-control="WinJS.UI.HtmlControl" data-win-options="{uri: '/pages/samplePageControl.html', controlText: 'This was rendered via the HtmlControl', message: 'HTML Control loaded control' }"></div>
摘要與後續步驟
您已了解如何建立和顯示 PageControl 物件。
如需如何使用 PageControl 物件的詳細資訊,請參閱快速入門:使用單頁瀏覽。