PointOfService 设备对象PointOfService device objects

创建设备对象Creating a device object

从更新枚举或存储的 DeviceID 确定了要使用的 PointOfService 设备后,你只需调用具有你以编程方式选择或者用户选择用来创建新服务点设备对象的 DeviceIDFromIdAsyncOnce you have identified the PointOfService device that you want to use, either from a fresh enumeration or a stored DeviceID, you just call FromIdAsync with theDeviceID that you have chosen programmatically or the user has selected to create a new Point of Service device object.

此示例尝试通过使用 DeviceID 的 FromIdAsync 创建新的 BarcodeScanner 对象。This sample attempts to create a new BarcodeScanner object with FromIdAsync using a DeviceID. 如果创建对象时出现问题,将写入调试消息。If there is a failure creating the object a debug message is written.

    BarcodeScanner barcodeScanner = await BarcodeScanner.FromIdAsync(DeviceId);

    if(barcodeScanner != null)
        // after successful creation, claim the scanner for exclusive use and enable it to exchange data
        Debug.WriteLine("Failure to create barcodeScanner object");

有了设备对象后,你可以访问设备的方法、属性和事件。Once you have a device object, you can then access the device's methods, properties and events.

设备对象生命周期Device object lifecycle

在 Windows 8 之前,应用的生命周期非常简单。Before Windows 8, apps had a simple lifecycle. Win32 和 .NET 应用正在运行或未运行,并且 PointOfService 外设通常是针对完整应用生命周期的要求。Win32 and .NET apps are either running or not running and PointOfService peripherals were usually claimed for the full app lifecycle. 当用户最小化应用或离开应用时,它们将继续运行。When a user minimizes them, or switches away from them, they continue to run. 随着便携设备和电源管理变得日益重要,这种情况不再可行。This was fine until portable devices and power management became increasingly important.

Windows 8 随 UWP 应用引入了新应用程序模型。Windows 8 introduced a new application model with UWP apps. 在高级别上,添加了新的已暂停状态。At a high level, a new suspended state was added. 当用户最小化 UWP 应用或切换到其他应用后,该应用会立刻处于暂停状态。A UWP app is suspended shortly after the user minimizes it or switches to another app. 这意味着应用的线程停止,应用保留在内存中,除非操作系统需要回收资源,并且表示 PointOfService 外设的任何设备对象都自动关闭以允许其他应用程序访问外设。This means that the app's threads are stopped, the app is left in memory unless the operating system needs to reclaim resources, and any device objects representing PointOfService peripherals are automatically closed to allow other applications access to the peripherals. 当用户切换回应用时,它可以快速还原到运行状态,并还原提供的 PointOfService 外设连接,它们在恢复后仍然可用。When the user switches back to the app, it can be quickly restored to a running state and restore PointOfService peripherals connections provided they are still available on resume.

你可以通过 <DeviceObject>.Closed 事件处理程序检测对象在何时因任何原因关闭,然后为在将来重新建立的连接记下设备 ID。You can detect when an object is closed for any reason with a <DeviceObject>.Closed event handler then make note of the device ID for re-establishing the connection in the future. 或者,你可能希望在应用挂起通知中处理此情况,以便保存设备 ID,以便在应用恢复通知时重新建立设备连接。Alternatively, you may wish to handle this on an App Suspend notification to save the device ID's for re-establishing the device connections on App Resume notification. 请确保不要在事件处理程序上两次处理,也不要对 <DeviceObject>.Closed 和应用暂停上的设备对象执行重复操作。Make sure that you do not double up on the event handlers and duplicate actions for the device object on both <DeviceObject>.Closed and App Suspend.


请参阅以下主题了解有关 Windows 10 通用 Windows 平台 (UWP) 应用程序生命周期的详细信息:Please refer to the following topics for more information about Windows 10 Universal Windows Platform (UWP) application lifecycle: