[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]
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 参数才能得到包含属性的容器枚举的结果。