Share via


快速入門:列舉裝置容器 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

PnpObjectType 列舉中的隨插即用 (PnP) 物件類型儲存了與特定裝置介面關聯的裝置資訊、介面所屬的裝置、裝置介面類別,或代表整個硬體產品的裝置容器。裝置容器描述裝置的可見部分,如製造商或型號名稱。Windows.Devices.Enumeration.DeviceInformation 代表的類型與 PnpObjectType.DeviceInterface 相同。

Windows.Devices.Enumeration.PnP 命名空間可讓您列舉裝置和裝置容器,以及裝置和裝置介面。 這個主題說明如何使用 Windows.Devices.Enumeration.PnP 命名空間來列舉裝置容器。

目標: 列舉裝置容器的屬性。

先決條件

您應該熟悉 JavaScript 和 HTML。

完成所需的時間: 20 分鐘.

指示

1. 開啟 Microsoft Visual Studio

開啟 Visual Studio 的執行個體。

2. 建立新專案

在 [新增專案] 對話方塊中,從 [JavaScript]**** > [Windows 市集應用程式] 專案類型按一下 [空白的應用程式]****。

3. 插入應用程式 HTML

開啟您的 Default.html,將這個程式碼複製至檔案,取代檔案的內容。


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="/js/default.js"></script>
</head>
<body data-design-activate="defaultPage.activated">
    <h1>Device Enumeration Sample</h1>

    <h2 >Input</h2>

    <div>            

       <div id="Input">
         <p>This scenario demonstrates enumerating device containers.</p>
          <p>Pressing the enumerate button will start a
             search for device containers.
             The containers will be listed below.</p>
          <input onclick="onEnumerateDeviceContainers()" type="button" value="Enumerate" />
                <br /><br />
       </div>
    </div>


    <h2> Output</h2>

            <div id="statusMessage"></div>

            <!-- Container Enumeration results are displayed in this element -->
            <div  id="output"></div>
</body>
</html>

4. 插入 JavaScript

在 Default.js 中插入這個程式碼。


function onEnumerateDeviceContainers() {
    try {

        document.getElementById("output").innerHTML = "";

        var propertiesToRetrieve = new Array();
        propertiesToRetrieve.push("System.ItemNameDisplay");
        propertiesToRetrieve.push("System.Devices.ModelName");
        propertiesToRetrieve.push("System.Devices.Manufacturer");

        var DevEnum = Windows.Devices.Enumeration;
        var Pnp = DevEnum.Pnp;
        var pnpObjType = Pnp.PnpObjectType;
        var deviceContainerType = pnpObjType.deviceContainer;

        Pnp.PnpObject.findAllAsync(
            deviceContainerType, 
            propertiesToRetrieve).then(
                function (devinfoCollection) {
                    var numDevices = devinfoCollection.length;
                    document.getElementById("statusMessage").innerHTML = 
                        numDevices + " device containers(s) found";
                    if (numDevices) {
                        for (var i = 0; i < numDevices; i++) {
                            printDeviceContainer(devinfoCollection[i], 
                            document.getElementById("output"));
                        }
                    } else {
                    document.getElementById("statusMessage").innerHTML = 
                        ("No devices found");
                    }
                },
                function (e) {
                    document.getElementById("statusMessage").innerHTML = 
                        ("Failed to find devices, error: " + e.message);
                });
    } catch (e) {
        document.getElementById("statusMessage").innerHTML = 
            ("Failed to enumerate devices, error: " + e.message);
    }
}


function printProperties(log, prop) {
    log.innerHTML += "property store count is: " + prop.size;
    var pt = prop.first();
    while (pt.hasCurrent) {
        log.innerHTML += "<br />" + pt.current.key + " := " + pt.current.value;
        pt.moveNext();
    }
    log.innerHTML += "<br />";
}

function printDeviceContainer(deviceContainer, log) {
    var prop = deviceContainer.properties;
    if (prop) {
        log.innerHTML += "<h3>" + prop.lookup("System.ItemNameDisplay") + "<h3/>";
        log.innerHTML += "<p>Container ID: " + deviceContainer.id + "<p/>";
        printProperties(log, prop);
    }
    log.innerHTML += "<br /><br />";
}

摘要

您剛剛列舉了裝置容器屬性。請注意,Windows.Devices.Enumeration.Pnp.findAllAsync 必須有 requestedProperties 參數,才能讓容器列舉的結果中包含屬性。