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 類別提供裝置資訊,但更具體來說,它會提供裝置介面的屬性,也就是代表裝置所公開功能的介面。 多函式裝置可能有一個以上的裝置介面。 使用者視為裝置的實體物件,稱為裝置容器,且具有 製造商 和 ModelID等屬性。 如需列舉裝置和復原屬性的詳細資訊,請參閱 列舉裝置。
屬性
EnclosureLocation |
裝置在其主機殼中的實體位置。 例如,它可能會描述膝上型電腦內網路攝影機的位置。 |
Id |
字串,表示裝置的身分識別。 |
IsDefault |
指出此裝置是否為 類別的預設裝置。 |
IsEnabled |
指出是否啟用此裝置。 |
Kind |
取得這個 物件所表示的 DeviceInformation 類型。 |
Name |
裝置的名稱。 此名稱是應用程式的最佳可用語言。 |
Pairing |
取得此裝置要配對之功能的相關資訊。 |
Properties |
包含已知值的屬性存放區,以及可在裝置列舉期間指定的其他屬性。 |