DeviceInformation Kelas

Definisi

Mewakili perangkat. Kelas ini memungkinkan akses ke properti perangkat terkenal serta properti tambahan yang ditentukan selama enumerasi perangkat.

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
Warisan
Object Platform::Object IInspectable DeviceInformation
Atribut

Persyaratan Windows

Rangkaian perangkat
Windows 10 (diperkenalkan dalam 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (diperkenalkan dalam v1.0)

Contoh

Contoh ini secara bertahap menghitung perangkat, menambahkannya ke daftar setiap kali perangkat ditemukan, dan juga menangani penghapusan dan pembaruan.

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);
        }
    }
}

Keterangan

Objek DeviceInformation terdiri dari identitas (DeviceInformation.Id), jenis (DeviceInformation.Kind), dan tas properti (DeviceInformation.Properties). Semua properti lain dari objek DeviceInformation berasal dari tas properti Properti . Misalnya, Nama berasal dari System.ItemNameDisplay.

Penyelesaian findAllAsync yang berhasil menghasilkan DeviceInformationCollection yang berisi objek DeviceInformation.

Jika panggilan ke CreateWatcher berhasil, objek DeviceInformation diteruskan ke peristiwa tambahan untuk setiap perangkat yang ditemukan.

Properti Nama hanya boleh digunakan untuk tujuan tampilan dan bukan untuk menemukan perangkat karena Nama dapat berubah karena pelokalan atau pengguna yang menetapkan nama.

CreateFromIdAsync membuat objek DeviceInformation jika berhasil.

Kelas DeviceInformation menyediakan informasi perangkat, tetapi lebih khusus lagi, kelas ini menyediakan properti antarmuka perangkat, antarmuka yang mewakili fungsionalitas yang diekspos perangkat. Perangkat multifungsi mungkin memiliki lebih dari satu antarmuka perangkat. Objek fisik yang dilihat pengguna sebagai perangkat, dikenal sebagai kontainer perangkat, dan memiliki properti seperti Produsen dan ModelID. Untuk informasi selengkapnya tentang menghitung perangkat dan memulihkan properti, lihat Menghitung perangkat.

Properti

EnclosureLocation

Lokasi fisik perangkat di penutupnya. Misalnya, ini dapat menggambarkan lokasi webcam di dalam laptop.

Id

String yang mewakili identitas perangkat.

IsDefault

Menunjukkan apakah perangkat ini adalah perangkat default untuk kelas .

IsEnabled

Menunjukkan apakah perangkat ini diaktifkan.

Kind

Mendapatkan tipe DeviceInformation yang diwakili oleh objek ini.

Name

Nama perangkat. Nama ini dalam bahasa terbaik yang tersedia untuk aplikasi.

Pairing

Mendapatkan informasi tentang kemampuan untuk dipasangkan perangkat ini.

Properties

Penyimpanan properti yang berisi nilai terkenal serta properti tambahan yang dapat ditentukan selama enumerasi perangkat.

Metode

CreateFromIdAsync(String)

Membuat objek DeviceInformation dari ID DeviceInformation .

CreateFromIdAsync(String, IIterable<String>)

Membuat objek DeviceInformation dari ID DeviceInformation dan daftar properti tambahan.

CreateFromIdAsync(String, IIterable<String>, DeviceInformationKind)

Membuat objek DeviceInformation dari ID DeviceInformation , daftar properti tambahan, dan parameter DeviceInformationKind .

CreateFromIdAsync(String, IIterable<String>, DeviceInformationKind, IDeviceEnumerationSettings)

Mewakili perangkat. Kelas ini memungkinkan akses ke properti perangkat terkenal serta properti tambahan yang ditentukan selama enumerasi perangkat.

CreateWatcher()

Membuat DeviceWatcher untuk semua perangkat.

CreateWatcher(DeviceClass)

Membuat DeviceWatcher untuk perangkat yang cocok dengan DeviceClass yang ditentukan.

CreateWatcher(String)

Membuat DeviceWatcher untuk perangkat yang cocok dengan string Advanced Query Syntax (AQS) yang ditentukan.

CreateWatcher(String, IIterable<String>)

Membuat DeviceWatcher untuk perangkat yang cocok dengan string Advanced Query Syntax (AQS) yang ditentukan dan kumpulan properti yang ditentukan.

CreateWatcher(String, IIterable<String>, DeviceInformationKind)

Membuat DeviceWatcher untuk perangkat yang cocok dengan string Advanced Query Syntax (AQS) yang ditentukan, kumpulan properti yang ditentukan, dan jenis perangkat.

CreateWatcher(String, IIterable<String>, DeviceInformationKind, IDeviceEnumerationSettings)

Mewakili perangkat. Kelas ini memungkinkan akses ke properti perangkat terkenal serta properti tambahan yang ditentukan selama enumerasi perangkat.

FindAllAsync()

Menghitung semua objek DeviceInformation .

FindAllAsync(DeviceClass)

Menghitung objek DeviceInformation dari kelas yang ditentukan.

FindAllAsync(String)

Menghitung objek DeviceInformation yang cocok dengan string pemilih antarmuka perangkat Advanced Query Syntax (AQS) yang ditentukan.

FindAllAsync(String, IIterable<String>)

Menghitung objek DeviceInformation yang cocok dengan string pemilih antarmuka perangkat Advanced Query Syntax (AQS) yang ditentukan dan termasuk kumpulan properti yang ditentukan.

FindAllAsync(String, IIterable<String>, DeviceInformationKind)

Menghitung objek DeviceInformation yang cocok dengan string pemilih antarmuka perangkat Advanced Query Syntax (AQS) yang ditentukan, jenis perangkat, dan termasuk kumpulan properti yang ditentukan.

FindAllAsync(String, IIterable<String>, DeviceInformationKind, IDeviceEnumerationSettings)

Mewakili perangkat. Kelas ini memungkinkan akses ke properti perangkat terkenal serta properti tambahan yang ditentukan selama enumerasi perangkat.

GetAqsFilterFromDeviceClass(DeviceClass)

Membuat filter yang akan digunakan untuk menghitung melalui subset jenis perangkat.

GetGlyphThumbnailAsync()

Mendapatkan glyph untuk perangkat.

GetThumbnailAsync()

Mengembalikan gambar mini untuk perangkat.

Update(DeviceInformationUpdate)

Updates properti objek DeviceInformation yang ada.

Berlaku untuk

Lihat juga