Session 02 kho tài liệu bách khoa

40 42 0
Session 02 kho tài liệu bách khoa

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Advanced Windows Store Apps Development – II  Enumerate Devices  Describe the Plug and Play (PnP) devices  Troubleshoot Asynchronous errors  Describe Sensors  Explain Location Services © Aptech Ltd Device Capabilities/Session 2 Microsoft insists their manufacturers and operators to create devices with better connectivity so that the device can be tracked anywhere The manufacturers also implement and follow these guidelines in case of internal as well as external devices they create Operators will provide the Geopositioning service to locate the device Whenever a user develops a store apps, they can also integrate it as a device app With device metadata, apps can automatically set up the device connected for the first time and provide more features than normal apps Apps can make use of features and utilize the variety of functionalities provided by devices © Aptech Ltd Device Capabilities/Session Windows.Devices.Enumeration and Windows.Devices.Enumeration.Pnp are the two namespaces to enumerate the devices connected or disconnected This Windows.Devices.Enumeration class provides two methods, namely FindAllAsync and CreateWatcher to enumerate the devices FindAllASync CreateWatcher • This will search the device only once as the device is connected • This will not update whenever the user adds or delete the devices • This will always enumerate devices by raising notification as the user adds or deletes the devices Note -The default class GUID for Printer: {0ECEF634-6EF0-472A-8085-5AD023ECBCCD} Webcam: {E5323777-F976-4F5B-9B55-B94699C46E44} Portable Devices: {6AC27878-A6FA-4155-BA85-F98F491D4F33} © Aptech Ltd Device Capabilities/Session Step 1: To get the GUID class of a device, the user has to insert any portable device such as pen drive, printer, or webcam Right-click the device and go to properties as shown in the following figure © Aptech Ltd Device Capabilities/Session Step 2: Go to Hardware tab, select the device, and go to Properties as shown in the following figure © Aptech Ltd Device Capabilities/Session Step 3: In the properties window, select events, and select the device to view the Class GUID as shown in the following figure © Aptech Ltd Device Capabilities/Session The DeviceWatcher class is in charge for device enumeration; floating exact events each time the devices are added, removed, or altered after the initial enumeration is finished The events of DeviceWatcher class are as follows: Added– Occurs when new device is found and added by the DeviceWatcher class EnumerationCompleted – Occurs when the device enumeration is completed Removed – Occurs when a device is removed Stopped – Occurs when enumeration process is stopped Updated – Occurs when enumeration process is modified © Aptech Ltd Device Capabilities/Session Plug and Play (PnP) is a mixture of hardware and software that enables a system to identify, setup, and get used to hardware structure modifications when any new device is connected by the user A user can connect or disconnect any number of devices to/from the computer These devices can be made available in the app by enabling the device in the app manifest file as shown in the following figure © Aptech Ltd Device Capabilities/Session To deliver a good user experience the app needs access to sensitive devices, it requires to handle the errors that can occur when trying to access a disabled device Reasons for not being able to access a device includes: The user may not have proper permissions to access the device The user may revoke the permission in the settings The device capability is not available on the system © Aptech Ltd Device Capabilities/Session 10 When the user writes the code given in Code Snippet in the design mode, the UI will be displayed as shown in the following figure © Aptech Ltd Device Capabilities/Session 26 Step 3: Add following code given in Code Snippet, in the code behind file, MainPage.xml.cs Code Snippet: using using using using using using using using using using using using using using using using © Aptech Ltd System; System; System.Collections.Generic; System.IO; System.Linq; System.Runtime.InteropServices.WindowsRuntime; Windows.Devices.Enumeration; Windows.Foundation; Windows.Foundation.Collections; Windows.UI.Xaml; Windows.UI.Xaml.Controls; Windows.UI.Xaml.Controls.Primitives; Windows.UI.Xaml.Data; Windows.UI.Xaml.Input; Windows.UI.Xaml.Media; Windows.UI.Xaml.Navigation; Device Capabilities/Session 27 Code Snippet (Cont.): namespace DeviceWatcherTest { public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } Windows.UI.Core.CoreDispatcher d; public static DeviceWatcher w = null; public static int c = 0; public static DeviceInformation[] i = new DeviceInformation[1000]; public static bool isComplete = false; public static string Status = null; © Aptech Ltd Device Capabilities/Session 28 The given Code Snippet function is to start the enumeration Code Snippet: async void fnDevicesWatch(object sender, RoutedEventArgs eventArgs) { try { d = Window.Current.CoreWindow.Dispatcher; w = DeviceInformation.CreateWatcher(); w.Added += watcher_Added; w.Removed += fnRemovedwatcher; w.Updated += watcher_Updated; w.EnumerationCompleted += fnEnumComplete; w.Stopped += fnStoppedwatcher; w.Start(); txtResult.Text = “Enumeration started.”; } catch (ArgumentException) { txtResult.Text = “Caught ArgumentException Failed to create watcher.”; } } © Aptech Ltd Device Capabilities/Session 29 The given Code Snippet function is to stop the enumeration Code Snippet: async void StopWatcher(object sender, RoutedEventArgs eventArgs) { try { if (w.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Stopped) { Status = “The enumeration is already stopped.”; } else { w.Stop(); } } catch (ArgumentException) { txtResult.Text = “Caught ArgumentException Failed to stop watcher.”; } } © Aptech Ltd Device Capabilities/Session 30 The given Code Snippet is to display the device list if the enumeration is completed Code Snippet: async void watcher_Added( DeviceWatcher sender, DeviceInformation deviceInterface) { i[c] = deviceInterface; c += 1; if (isComplete) { await d.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { fnDisplayDeviceList(); }); } } © Aptech Ltd Device Capabilities/Session 31 The given Code Snippet is used to update the device list if the enumeration is completed Code Snippet: async void watcher_Updated(DeviceWatcher sender, DeviceInformationUpdate devUpdate) { int count2 = 0; foreach (DeviceInformation deviceInterface in i) { if (count2 < c) { if (i[count2].Id == devUpdate.Id) { //Update the element i[count2].Update( devUpdate); } } count2 += 1; © Aptech Ltd Device Capabilities/Session 32 Code Snippet (Cont.): } await d.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { txtResult.Text = “Enumeration updated “; fnDisplayDeviceList(); }); } The given Code Snippet is used to remove the existing device list if the enumeration is completed Code Snippet: async void fnRemovedwatcher(DeviceWatcher sender, DeviceInformationUpdate devUpdate) { int count2 = 0; //Convert interfaces array to a list ( IList) List interfaceList = new List(i); © Aptech Ltd Device Capabilities/Session 33 Code Snippet (Cont.): foreach (DeviceInformation deviceInterface in i) { if (count2 < c) { if (i[count2].Id == devUpdate.Id) { //Remove the element interfaceList.RemoveAt( count2); } } count2 += 1; } //Convert the list back to the interfaces array i = interfaceList.ToArray(); c -= 1; await d.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { © Aptech Ltd Device Capabilities/Session 34 Code Snippet (Cont.): txtResult.Text = “Enumeration device was removed “; fnDisplayDeviceList(); }); } async void fnEnumComplete( DeviceWatcher sender, object args) { isComplete = true; await d.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { txtResult.Text = “Enumeration complete “; fnDisplayDeviceList(); }); } © Aptech Ltd Device Capabilities/Session 35 The given Code Snippet is used to stop the enumeration Code Snippet: async void fnStoppedwatcher(DeviceWatcher sender, object args) { if (w.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Aborted) { Status = “Enumeration stopped.”; } } async void fnDisplayDeviceList() { lstResult.Items.Clear(); int count2 = 0; foreach (DeviceInformation deviceInterface in i) { if (count2 < c) { fnDisplayDeviceInterface( deviceInterface); } count2 += 1; }} © Aptech Ltd Device Capabilities/Session 36 The given Code Snippet is used to display device information Code Snippet: async void fnDisplayDeviceInterface(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; lstResult.Items.Add(item); } } } © Aptech Ltd Device Capabilities/Session 37 Step 4: The output screen to display the page when the user executes the app as shown in the following figure © Aptech Ltd Device Capabilities/Session 38 Step 5: The output screen to display the device information when the user clicks watch all device button as shown in figure © Aptech Ltd Device Capabilities/Session 39  Windows.Devices.Enumeration and Windows.Devices.Enumeration.Pnp are the two namespaces to enumerate the devices connected or disconnected  To enumerate devices the user has to first identify the GUID class for the device to be tested  The DeviceWatcher class is in charge for device enumeration with passion; floating exact events each time the devices are added, removed, or altered after the initial enumeration is finished  Some of the competence acknowledged in the app manifest file such as microphone, camera, and the location can expose private information  Windows.Devices.Sensors namespace is used to capture motion, device orientation or light sensors  To make use of this orientation the user has to select the supported rotations from the application tab in the app manifest file © Aptech Ltd Device Capabilities/Session 40 ... {0ECEF634-6EF0-472A-8085-5AD023ECBCCD} Webcam: {E5323777-F976-4F5B-9B55-B94699C46E44} Portable Devices: {6AC27878-A6FA-4155-BA85-F98F491D4F33} © Aptech Ltd Device Capabilities /Session Step 1: To get... Ltd Device Capabilities /Session Step 2: Go to Hardware tab, select the device, and go to Properties as shown in the following figure © Aptech Ltd Device Capabilities /Session Step 3: In the properties... Capabilities /Session 25 When the user writes the code given in Code Snippet in the design mode, the UI will be displayed as shown in the following figure © Aptech Ltd Device Capabilities /Session 26

Ngày đăng: 08/11/2019, 18:06

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan