DeviceInformation 类

定义

表示设备。 此类允许访问已知设备属性以及设备枚举期间指定的其他属性。

public ref class DeviceInformation sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class DeviceInformation final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class DeviceInformation
Public NotInheritable Class DeviceInformation
继承
Object IInspectable DeviceInformation
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

此示例以增量方式枚举设备,每次找到设备时将它们添加到列表中,以及处理删除和更新。

using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Imaging;

using Windows.Devices.Enumeration;
using Windows.Devices.Enumeration.Pnp;


// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Application1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 
    public sealed partial class BlankPage : Page
    {
        public BlankPage()
        {

            this.InitializeComponent();
        }
        Windows.UI.Core.CoreDispatcher dispatcher;
        public static DeviceWatcher watcher = null;
        public static int count = 0;
        public static DeviceInformation[] interfaces = new DeviceInformation[1000];
        public static bool isEnumerationComplete = false;
        public static string StopStatus = null;

        async void WatchDevices(object sender, RoutedEventArgs eventArgs)
        {
            try
            {
                dispatcher = Window.Current.CoreWindow.Dispatcher;
                watcher = DeviceInformation.CreateWatcher();
                // Add event handlers
                watcher.Added += watcher_Added;
                watcher.Removed += watcher_Removed;
                watcher.Updated += watcher_Updated;
                watcher.EnumerationCompleted += watcher_EnumerationCompleted;
                watcher.Stopped += watcher_Stopped;
                watcher.Start();
                OutputText.Text = "Enumeration started.";

            }
            catch (ArgumentException)
            {
                //The ArgumentException gets thrown by FindAllAsync when the GUID isn't formatted properly
                //The only reason we're catching it here is because the user is allowed to enter GUIDs without validation
                //In normal usage of the API, this exception handling probably wouldn't be necessary when using known-good GUIDs 
                OutputText.Text = "Caught ArgumentException. Failed to create watcher.";
            }
        }

        async void StopWatcher(object sender, RoutedEventArgs eventArgs)
        {
            try
            {
                if (watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Stopped)
                {
                    StopStatus = "The enumeration is already stopped.";
                }
                else
                {
                    watcher.Stop();
                }
            }
            catch (ArgumentException)
            {
                OutputText.Text = "Caught ArgumentException. Failed to stop watcher.";
            }
        }

        async void watcher_Added(DeviceWatcher sender, DeviceInformation deviceInterface)
        {
            interfaces[count] = deviceInterface;
            count += 1;
            if (isEnumerationComplete)
            {
                await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    DisplayDeviceInterfaceArray();
                });
            }
        }

        async void watcher_Updated(DeviceWatcher sender, DeviceInformationUpdate devUpdate)
        {
            int count2 = 0;
            foreach (DeviceInformation deviceInterface in interfaces)
            {
                if (count2 < count)
                {
                    if (interfaces[count2].Id == devUpdate.Id)
                    {
                        //Update the element.
                        interfaces[count2].Update(devUpdate);
                    }

                }
                count2 += 1;
            }
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                OutputText.Text = "Enumeration updated. ";
                DisplayDeviceInterfaceArray();
            });
        }

        async void watcher_Removed(DeviceWatcher sender, DeviceInformationUpdate devUpdate)
        {
            int count2 = 0;
            //Convert interfaces array to a list (IList).
            List<DeviceInformation> interfaceList = new List<DeviceInformation>(interfaces);
            foreach (DeviceInformation deviceInterface in interfaces)
            {
                if (count2 < count)
                {
                    if (interfaces[count2].Id == devUpdate.Id)
                    {
                        //Remove the element.
                        interfaceList.RemoveAt(count2);
                    }

                }
                count2 += 1;
            }
            //Convert the list back to the interfaces array.
            interfaces = interfaceList.ToArray();
            count -= 1;
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                OutputText.Text = "Enumeration device was removed. ";
                DisplayDeviceInterfaceArray();
            });
        }

        async void watcher_EnumerationCompleted(DeviceWatcher sender, object args)
        {
            isEnumerationComplete = true;
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    OutputText.Text = "Enumeration complete. ";
                    DisplayDeviceInterfaceArray();
                });
        }

        async void watcher_Stopped(DeviceWatcher sender, object args)
        {
            if (watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Aborted)
            {
                StopStatus = "Enumeration stopped unexpectedly. Click Watch to restart enumeration.";
            }
            else if (watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Stopped)
            {
                StopStatus = "You requested to stop the enumeration. Click Watch to restart enumeration.";
            }
        }

        async void DisplayDeviceInterfaceArray()
        {
            DeviceInterfacesOutputList.Items.Clear();
            int count2 = 0;
            foreach (DeviceInformation deviceInterface in interfaces)
            {
                if (count2 < count)
                {
                    DisplayDeviceInterface(deviceInterface);
                }
                count2 += 1;
            }
        }

        async void DisplayDeviceInterface(DeviceInformation deviceInterface)
        {
            var id = "Id:" + deviceInterface.Id;
            var name = deviceInterface.Name;
            var isEnabled = "IsEnabled:" + deviceInterface.IsEnabled;


            var item = id + " is \n" + name + " and \n" + isEnabled;

            DeviceInterfacesOutputList.Items.Add(item);
        }
    }
}

注解

DeviceInformation 对象由标识 (DeviceInformation.Id)、种类 (DeviceInformation.Kind) 和属性包 (DeviceInformation.Properties) 组成。 DeviceInformation 对象的所有其他属性都派生自 Properties 属性包。 例如,Name 派生自 System.ItemNameDisplay

FindAllAsync 成功完成会导致包含 DeviceInformation 对象的 DeviceInformationCollection

如果对 CreateWatcher 的调用成功,则会将 DeviceInformation 对象传递给找到的每个设备的 添加 事件。

Name 属性应仅用于显示目的,而不应用于查找设备,因为名称可能因本地化或分配名称的用户而更改。

CreateFromIdAsync 如果成功,则创建 DeviceInformation 对象。

DeviceInformation 类提供设备信息,但更具体地说,它提供设备接口的属性,即表示设备公开的功能的接口。 多功能设备可能有多个设备接口。 用户视为设备的物理对象称为设备容器,并具有 制造商ModelID 等属性。 有关枚举设备和恢复属性的详细信息,请参阅 枚举设备

属性

EnclosureLocation

设备的物理位置在其机箱中。 例如,它可能描述笔记本电脑内网络摄像头的位置。

Id

表示设备的标识的字符串。

IsDefault

指示此设备是否为类的默认设备。

IsEnabled

指示是否启用此设备。

Kind

获取此对象表示的 DeviceInformation 的类型。

Name

设备的名称。 此名称是应用的最佳可用语言。

Pairing

获取有关此设备要配对的功能的信息。

Properties

包含已知值以及可在设备枚举期间指定的其他属性的属性存储。

方法

CreateFromIdAsync(String)

DeviceInformation ID 创建 DeviceInformation 对象。

CreateFromIdAsync(String, IIterable<String>)

DeviceInformation ID 和附加属性列表创建 DeviceInformation 对象。

CreateFromIdAsync(String, IIterable<String>, DeviceInformationKind)

DeviceInformation ID、附加属性列表和 DeviceInformationKind 参数创建 DeviceInformation 对象。

CreateWatcher()

为所有设备创建 DeviceWatcher

CreateWatcher(DeviceClass)

为匹配指定 DeviceClass 的设备创建 DeviceWatcher

CreateWatcher(String)

为与指定的高级查询语法匹配的设备创建 DeviceWatcher (AQS) 字符串。

CreateWatcher(String, IIterable<String>)

为与指定的高级查询语法匹配的设备创建 DeviceWatcher (AQS) 字符串和指定的属性集合。

CreateWatcher(String, IIterable<String>, DeviceInformationKind)

为与指定的高级查询语法匹配的设备创建 DeviceWatcher (AQS) 字符串、属性的指定集合以及设备类型。

FindAllAsync()

枚举所有 DeviceInformation 对象。

FindAllAsync(DeviceClass)

枚举指定类的 DeviceInformation 对象。

FindAllAsync(String)

枚举与指定的高级查询语法匹配的 DeviceInformation 对象, (AQS) 设备接口选择器字符串。

FindAllAsync(String, IIterable<String>)

枚举与指定的高级查询语法匹配的 DeviceInformation 对象 (AQS) 设备接口选择器字符串,包括指定的属性集合。

FindAllAsync(String, IIterable<String>, DeviceInformationKind)

枚举与指定的高级查询语法匹配的 DeviceInformation 对象 (AQS) 设备接口选择器字符串、设备类型,以及包括指定的属性集合。

GetAqsFilterFromDeviceClass(DeviceClass)

创建用于枚举设备类型的子集的筛选器。

GetGlyphThumbnailAsync()

获取设备的标志符号。

GetThumbnailAsync()

返回设备的缩略图。

Update(DeviceInformationUpdate)

汇报现有 DeviceInformation 对象的属性。

适用于

另请参阅