簡體   English   中英

如何在 UWP 中獲取連接的掃描儀列表

[英]How to get connected scanner list in UWP

我想獲取連接的掃描儀列表(名稱)並將掃描儀名稱綁定到組合框,以進行掃描。 我是 UWP 的新手,請分享任何鏈接或代碼。

我剛剛從 NuGet 包安裝了 NTwain 庫。

我想在頁面加載時將掃描儀名稱綁定到組合框(通過創建 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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM