Xamarin Form - 在 ScanFilesToFolderAsync 上抛出 TaskCanceledException
Xamarin Form - TaskCanceledException thrown on ScanFilesToFolderAsync
在我的 UWP 应用程序中,我正在处理扫描功能。在此应用程序中,用户可以通过选择平板或自动扫描仪扫描文档 feeder.Now 问题是当我尝试扫描它时出现异常,任务被取消。
请帮忙..
提前致谢。 :)
祝你有美好的一天...:)
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 中出错: 'Exception from HRESULT: 0x80210015' 表示未找到设备。
现在我将全部更改为 ImageScanner,我在扫描仪列表中什么也没有。
并且在扫描仪 HP 应用程序中我得到了这个名字。和 IT Scan Well :( 在扫描仪列表中,我的应用程序中没有这个名称。:(
在我的电脑设置 -> 设备 -> 扫描仪和打印机上,我得到了这些名称。
重写问题的解决方案作为答案。我在我的机器上测试了运行正确的代码,并认为问题很可能是驱动程序问题。这已得到 OP 的确认,重新安装驱动程序有助于再次扫描。
在我的 UWP 应用程序中,我正在处理扫描功能。在此应用程序中,用户可以通过选择平板或自动扫描仪扫描文档 feeder.Now 问题是当我尝试扫描它时出现异常,任务被取消。
请帮忙..
提前致谢。 :)
祝你有美好的一天...:)
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 中出错: 'Exception from HRESULT: 0x80210015' 表示未找到设备。 现在我将全部更改为 ImageScanner,我在扫描仪列表中什么也没有。
并且在扫描仪 HP 应用程序中我得到了这个名字。和 IT Scan Well :( 在扫描仪列表中,我的应用程序中没有这个名称。:(
在我的电脑设置 -> 设备 -> 扫描仪和打印机上,我得到了这些名称。
重写问题的解决方案作为答案。我在我的机器上测试了运行正确的代码,并认为问题很可能是驱动程序问题。这已得到 OP 的确认,重新安装驱动程序有助于再次扫描。