如何抓取相關的 PnP 物件 (HTML)

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

本主題說明如何尋找彼此關聯的隨插即用 (PnP) 物件。 例如,您可能想抓取和某個裝置資訊物件相關的裝置容器物件。 裝置容器代表使用者看見的實體裝置。裝置容器可讓您存取和整個裝置硬體產品相關的資訊,而非僅功能介面的其中之一。裝置容器屬性的範例為製造商或型號名稱。

了解 PnP 物件的關係

這個主題中的表格顯示不同 PnpObjectType 物件之間的關係,以及在物件之間瀏覽時必須使用的屬性。 請注意,DeviceInformation 物件等同於 PnpObject (PnpObjectType 等於 deviceInterface)。

多對一關係

本節中的表格顯示如何從與其他物件具有 N 對 1 關係的物件開始瀏覽。例如,一或多個裝置介面 (由 DeviceInformation 物件表示) 可能屬於相同的裝置容器。 前兩欄的標題包含您開始的隨插即用 (PnP) 物件類型。第三欄中的項目包含想要尋找的相關 PnP 物件。表格中的儲存格 (如果有資料) 包含可以用來查詢相關 PnP 物件的屬性。

若要從欄標題中列示的物件瀏覽到第三欄中列示的物件,請執行下列動作:

  1. 在直欄 (與您的開始物件對應的標題) 以及在要尋找相關物件的橫列中尋找屬性。
  2. 使用如何抓取相關的 PnP 物件中所述的步驟抓取屬性。
  3. PnpObject.createFromIdAsync 呼叫中將抓取到的值當作 id 參數,以建立相關類型的物件。
裝置介面 (DeviceInformation) 裝置 相關物件
System.Devices.ContainerId System.Devices.ContainerId deviceContainer
System.Devices.DeviceInstancePath 裝置
System.Devices.DeviceInterfaceClassGuid deviceInterfaceClass

 

一對多關係

本節中的表格顯示如何從與其他物件具有 1 對 N 關係的物件開始瀏覽。 若要從開始的物件 (在最上層列) 瀏覽到右邊列示的相關物件,請執行下列動作:

  1. 在直欄 (您的開始物件標題) 以及在要尋找相關物件的橫列中尋找屬性。

  2. 使用您的開始物件識別碼 (標題列中) 和您在表格中識別的屬性,構成格式如下的 AQS 字串:"<property>:=<id>"。

    例如,如果我們想在身分識別為 "{1451362b-4b9c-4780-a4bf-6c001619646c}" 的容器中尋找所有介面,則 AQS 字串為 "System.Devices.ContainerId:={1451362b-4b9c-4780-a4bf-6c001619646c}"。

  3. Windows.Devices.Enumeration.Pnp.PnpObject.findAllAsync 呼叫中將 AQS 字串當作參數,以尋找相關類型的物件。

    注意  使用含有 GUID 的屬性時,在構成 AQS 字串時,必須將 GUID 值放在大括弧中。

     

deviceContainer 裝置 deviceInterfaceClass 相關物件
System.Devices.ContainerId System.Devices.DeviceInstancePath System.Devices.DeviceInterfaceClassGuid deviceInterface
System.Devices.ContainerId System.Devices.DeviceInstancePath 裝置

 

範例

從 DeviceInformation 識別碼取得容器

您可能需要取得裝置介面所屬容器的屬性。例如,如果您有所選裝置的裝置識別碼,但需要取得型號名稱,則您需要建立容器物件,因為 System.Devices.ModelName 是容器屬性而不是裝置介面屬性。

這個程式碼顯示如何取得開頭是裝置識別碼的容器屬性。


// GetModelNameFromDeviceID gets the ModelName property from the
// device interface's container.
// The parameter devID is assumed to be a valid device interface ID.
string GetModelNameFromDeviceID (devID)
{
// First create a DeviceInformation object from the ID.
// Create the argument that specifies that additional properties
// returned should include the System.Devices.ContainerId property.
   var propertiesToGet = new Array();
   propertiesToGet.push("System.Devices.ContainerId");


// Create a DeviceInformation object from the ID.
   var Enum = Windows.Devices.Enumeration;
   var DevInfo = Enum.DeviceInformation;
   var contID;   // The container ID
   DevInfo.createFromIdAsync(devID, propertiesToGet).then(
       function(devInf) {             
            var prop = devInf.properties;
            if (prop) {
                contID = prop.lookup("System.Devices.ContainerID");               
            }
       },
       function (e) {
           displayError("Failed to create DeviceInformation: " + e.message);
       });

// Use the container ID to create a PnPObject representing the container,
// and specify that the created object should have a 
// System.Devices.ModelId property.


// Create the argument that specifies that the additional properties to
// return should include the System.Devices.ContainerId property.
   var containerPropertiesToGet = new Array();
   containerPropertiesToGet.push("System.Devices.ModelId");

   var modelID;


var Pnp = Enum.Pnp;
var pnpObjType = Pnp.PnpObjectType;
var deviceType = pnpObjType.device;

// NOTE: We need to add extra braces "{}" back to the container ID before
// passing it to createIdAsync (a pair of braces is lost in the implicit
// conversion from GUID to string).
   contID = "{" + contID + "}";

// Create the container from its ID.
   Pnp.createFromIdAsync(deviceType, contID, containerPropertiesToGet).then(
       function(contInf) {
           var prop = contInf.properties;
           if (prop) {
               modelID = prop.lookup("System.Devices.ModelID");
           });
   return modelID;
}

注意  

如果您將屬性查詢的 GUID 結果傳送到將 GUID 當作字串引數的函式,則在上述範例中,您需要先在 GUID 引數前後另外加上括號 "{}",然後再將它傳送到函式。這是因為屬性查詢結果為 variant 類型而不是字串,所以當引數傳送到接受字串的函式時,會在隱含轉換時遺失一對括號。

 

取得特定裝置容器中的所有 DeviceInformation 物件

這個範例示範如何在指定的裝置容器中尋找所有的 DeviceInformation 物件,方法就是建立 AQS 查詢,然後將查詢傳送到 findAllAsync

function findInterfacesInContainer(containerId) {

    var Enum = Windows.Devices.Enumeration;
    var aqsString = "System.Devices.ContainerId:=\"" + containerId + "\"";

    Enum.DeviceInformation.findAllAsync(aqsString, null).then(
            successCallback, 
            errorCallback);
}

// Handles successful completion of the findAllAsync method.
function successCallback(deviceInformationCollection) {
    // Add your code here for processing the enumerated device collection.
}

// Handles an error completion of the findAllAsync method.
function errorCallback(e) {
    // Add your error-handling code here.
}

相關主題

如何抓取裝置或 PnP 物件的其他屬性

AQS 查詢