DeviceInformation 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示设备。 此类允许访问已知的设备属性,以及设备枚举期间指定的其他属性。
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
- 继承
- 属性
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 类提供设备信息,但更具体地说,它提供设备接口的属性,即表示设备公开的功能的接口。 多功能设备可能有多个设备接口。 用户视为设备的物理对象称为设备容器,具有 Manufacturer 和 ModelID 等属性。 有关枚举设备和恢复属性的详细信息,请参阅 枚举设备。
属性
EnclosureLocation |
设备在其机箱中的物理位置。 例如,它可能描述笔记本电脑内网络摄像头的位置。 |
Id |
表示设备标识的字符串。 |
IsDefault |
指示此设备是否为 类的默认设备。 |
IsEnabled |
指示此设备是否已启用。 |
Kind |
获取由此 对象表示的 DeviceInformation 的类型。 |
Name |
设备的名称。 此名称采用应用的最佳可用语言。 |
Pairing |
获取有关此设备要配对的功能的信息。 |
Properties |
包含已知值以及可在设备枚举期间指定的其他属性的属性存储。 |