Xamarin表单 - 在ScanFilesToFolderAsync上抛出TaskCanceledException

问题描述 投票:2回答:1

在我的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的所有内容都没有在扫描仪列表中得到任何东西。

enter image description here

在扫描仪HP应用程序中我得到了这个名字。和IT扫描井:(在扫描仪列表中,我的应用程序中没有这个名称。:( enter image description here

在我的电脑设置 - >设备 - >扫描仪和打印机我有这个名字。

enter image description here

c# xamarin.forms uwp win-universal-app
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.