【问题标题】:Xamarin Form - TaskCanceledException thrown on ScanFilesToFolderAsyncXamarin 表单 - 在 ScanFilesToFolderAsync 上引发 TaskCanceledException
【发布时间】:2019-09-05 06:00:12
【问题描述】:

在我的 UWP 应用程序中,我正在研究扫描功能。在此应用程序中,用户可以通过选择平板或自动进纸器来通过扫描仪扫描文档。现在的问题是,当我尝试扫描时,它会给出一个任务被取消的异常。

请帮忙..

提前致谢。 :)

祝你有美好的一天...... :)

private async void Btnscan_Click(object sender, RoutedEventArgs e)
    {

        FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        folderPicker.FileTypeFilter.Add("*");

        StorageFolder folder = await folderPicker.PickSingleFolderAsync();
//set the destination folder name for scan images.


        DeviceInformationDisplay selectedScanner = CmbScannerList.SelectedItem as DeviceInformationDisplay; // here i got the selected scanner.

        // scanner id is := "\\\\?\\ROOT#IMAGE#0000#{6bdd1fc6-810f-11d0-bec7-08002be2092f}"

        ScanToFolder(selectedScanner.id, folder);


    }

功能扫描到文件夹

 public async void ScanToFolder(string deviceId, StorageFolder folder)
    {
        try
        {
 cancellationToken = new CancellationTokenSource();

            ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

            if (myScanner.IsScanSourceSupported(ImageScannerScanSource.Flatbed))
            {
                var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Flatbed, folder).AsTask(cancellationToken.Token); // through an exception(A Task Was Canceled):(
Utils.DisplayImageAndScanCompleteMessage(result.ScannedFiles, DisplayImage);

            }

        }

        catch (Exception ex)
        {
            // here i got the exception.
        }

    }

更新:

现在我将 DeviceClass 设置为 ALL。

   private void StartWatcher()
    {
        resultCollection.Clear();
        DeviceWatcher deviceWatcher;

        deviceWatcher = DeviceInformation.CreateWatcher(DeviceClass.All); // set Image scanner to all. 
       deviceWatcherHelper.StartWatcher(deviceWatcher);
  }

在扫描仪列表中运行项目后,我得到了所有连接的设备,其中我得到了我的扫描仪名称:当我尝试传递此名称时,它在 imagescanner System.Exception 中给出错误:'HRESULT 的异常:0x80210015'表示找不到设备。 现在我将全部更改为 ImageScanner 我在扫描仪列表中一无所有。

在扫描仪 HP 应用程序中,我得到了这个名称。和 IT Scan Well :( 在扫描仪列表中,我的应用程序中没有这个名称。:(

在我的电脑设置 -> 设备 -> 扫描仪和打印机上,我得到了这些名称。

【问题讨论】:

  • cancellationToken 在哪里声明?是不是真的取消了?
  • 我已经在我的机器上测试了你的项目,它似乎按预期工作,我没有得到TaskCanceledException。也许您的扫描仪驱动程序有问题?您可以在其他机器上或使用其他扫描仪进行测试吗?
  • 您好,抱歉回复晚了,TWAIN.dll 不能直接在 Xamarin.Forms 和 UWP 中使用,但您可以使用桌面扩展 (stefanwick.com/2018/04/06/uwp-with-desktop-extension-part-2) 启动完全信任程序集并与之通信。
  • 我有一台带扫描仪的三星打印机
  • 太棒了!我已将解决方案重写为答案,请考虑接受它以解决问题。编码愉快!

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


【解决方案1】:

重写问题的解决方案作为答案。我在我的机器上测试了正确运行的代码,并认为问题很可能是驱动程序问题。 OP 已确认这一点,重新安装驱动程序有助于使扫描再次工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2019-10-21
    • 2019-08-28
    • 2016-01-11
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多