【发布时间】: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