【问题标题】:How to get connected scanner list in UWP如何在 UWP 中获取已连接的扫描仪列表
【发布时间】:2019-08-30 09:49:17
【问题描述】:

我想获取连接的扫描仪列表(名称)并将扫描仪名称绑定到组合框,以进行扫描。我是 UWP 新手,请分享任何链接或代码。

我刚刚从 NuGet 包安装了 NTwain 库。

【问题讨论】:

标签: c# xamarin.forms uwp win-universal-app


【解决方案1】:

我想在页面加载时将扫描仪名称绑定到组合框(通过创建 getScannerName 方法)怎么做?

根据您的要求,您可以使用DeviceWatcher 枚举所有ImageScanner。然后将结果绑定到ListView。更多细节可以参考DeviceEnumerationAndPairing官方代码示例场景2。

private void StartWatcher()
    {
        startWatcherButton.IsEnabled = false;
        ResultCollection.Clear();

        // First get the device selector chosen by the UI.
        DeviceSelectorInfo deviceSelectorInfo = (DeviceSelectorInfo)selectorComboBox.SelectedItem;

        if (null == deviceSelectorInfo.Selector)
        {
            // If the a pre-canned device class selector was chosen, call the DeviceClass overload
            deviceWatcher = DeviceInformation.CreateWatcher(deviceSelectorInfo.DeviceClassSelector);
        }
        else if (deviceSelectorInfo.Kind == DeviceInformationKind.Unknown)
        {
            // Use AQS string selector from dynamic call to a device api's GetDeviceSelector call
            // Kind will be determined by the selector
            deviceWatcher = DeviceInformation.CreateWatcher(
                deviceSelectorInfo.Selector, 
                null // don't request additional properties for this sample
                );
        }
        else
        {
            // Kind is specified in the selector info
            deviceWatcher = DeviceInformation.CreateWatcher(
                deviceSelectorInfo.Selector,
                null, // don't request additional properties for this sample
                deviceSelectorInfo.Kind);
        }

        // Hook up handlers for the watcher events before starting the watcher

        handlerAdded = new TypedEventHandler<DeviceWatcher, DeviceInformation>(async (watcher, deviceInfo) =>
        {
            // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
            await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ResultCollection.Add(new DeviceInformationDisplay(deviceInfo));

                rootPage.NotifyUser(
                    String.Format("{0} devices found.", ResultCollection.Count),
                    NotifyType.StatusMessage);
            });
        });
        deviceWatcher.Added += handlerAdded;

        handlerUpdated = new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>(async (watcher, deviceInfoUpdate) =>
        {
            // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
            await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                // Find the corresponding updated DeviceInformation in the collection and pass the update object
                // to the Update method of the existing DeviceInformation. This automatically updates the object
                // for us.
                foreach (DeviceInformationDisplay deviceInfoDisp in ResultCollection)
                {
                    if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
                    {
                        deviceInfoDisp.Update(deviceInfoUpdate);
                        break;
                    }
                }
            });
        });
        deviceWatcher.Updated += handlerUpdated;

        handlerRemoved = new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>(async (watcher, deviceInfoUpdate) =>
        {
            // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
            await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                // Find the corresponding DeviceInformation in the collection and remove it
                foreach (DeviceInformationDisplay deviceInfoDisp in ResultCollection)
                {
                    if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
                    {
                        ResultCollection.Remove(deviceInfoDisp);
                        break;
                    }
                }

                rootPage.NotifyUser(
                    String.Format("{0} devices found.", ResultCollection.Count), 
                    NotifyType.StatusMessage);
            });
        });
        deviceWatcher.Removed += handlerRemoved;

        handlerEnumCompleted = new TypedEventHandler<DeviceWatcher, Object>(async (watcher, obj) =>
        {
            await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                rootPage.NotifyUser(
                    String.Format("{0} devices found. Enumeration completed. Watching for updates...", ResultCollection.Count),
                    NotifyType.StatusMessage);
            });
        });
        deviceWatcher.EnumerationCompleted += handlerEnumCompleted;

        handlerStopped = new TypedEventHandler<DeviceWatcher, Object>(async (watcher, obj) =>
        {
            await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                rootPage.NotifyUser(
                    String.Format("{0} devices found. Watcher {1}.", 
                        ResultCollection.Count,
                        DeviceWatcherStatus.Aborted == watcher.Status ? "aborted" : "stopped"),
                    NotifyType.StatusMessage);
            });
        });
        deviceWatcher.Stopped += handlerStopped;

        rootPage.NotifyUser("Starting Watcher...", NotifyType.StatusMessage);
        deviceWatcher.Start();
        stopWatcherButton.IsEnabled = true;
    }

【讨论】:

  • 非常感谢您的宝贵时间和回答。 :) 现在可以了。你知道如何通过 uwp 中的自动进纸器扫描仪扫描图像吗?请分享链接或代码。再次感谢你:)
  • 请查看document
  • 这是 windows 8 代码sample,您可以参考它的代码。
  • 谢谢 :) 您是否尝试过使用平板扫描仪进行扫描的示例代码?
  • 抱歉,我们没有平板扫描仪这样的设备。
猜你喜欢
  • 1970-01-01
  • 2012-06-08
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 2021-01-13
  • 2022-01-16
  • 2018-04-25
相关资源
最近更新 更多