[UWP]错误报告:ImageScannerScanResult.ScannedFiles抛出InvalidCastException [英] [UWP] Bug Report: ImageScannerScanResult.ScannedFiles throws InvalidCastException

查看:87
本文介绍了[UWP]错误报告:ImageScannerScanResult.ScannedFiles抛出InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个UWP应用程序,我需要使用Windows.Devices.Scanners.ImageScanner类扫描图像。基本上,这些是以下步骤:


  1. 使用DevicePicker类获取设备ID
  2. 使用ImageScanner.FromIdAsync构建ImageScanner(... )
  3. 调用ScanFilesToFolderAsync(...)扫描图像并将其保存到临时文件夹(ApplicationData.Current.TemporaryFolder)。这将返回一个ImageScannerScanResult。
  4. 使用ImageScannerScanResult.ScannedFiles获取图像文件


步骤1 - 3正常工作,然后在步骤4中发生InvalidCastException:

无法将类型为'System .__ ComObject'的对象强制转换为'System.Runtime.InteropServices.WindowsRuntime.IIterable`1 [ Windows.Storage.StorageFile]'。

解决方案

你好SvenEV,


施法错误似乎告诉我们问题不是已知问题。


您可以试试官方样本:
https://code.msdn.microsoft.com/windowsapps/Scan-Runtime-API-Sample-5703b7a2/sourcecode?fileId=86722&pathId=1503360506
并测试是否你可以先没问题地运行它吗?


我没有设备可以帮助你测试这个问题,但是请检查
的返回结果
ScanFilesToFolderAsync | scanFilesToFolderAsync方法
。这是
IAsyncOperationWithProgress< ImageScannerScanResult,UInt32>


因此,在我们的官方样本中,您将看到以下代码:


https://code.msdn.microsoft.com/windowsapps/Scan-Runtime-API-Sample-5703b7a2/ sourcecode?fileId = 86722& pathId = 1503360506

 var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Default,folder).AsTask(cancellationToken.Token,进展); 
ModelDataContext.ScenarioRunning = false;
if(result.ScannedFiles.Count> 0)
{
Utils.DisplayImageAndScanCompleteMessage(result.ScannedFiles,DisplayImage);
if(result.ScannedFiles.Count> 1)
{
Utils.UpdateFileListData(result.ScannedFiles,ModelDataContext);
}
}
else
{
rootPage.NotifyUser("没有扫描文件。",NotifyType.ErrorMessage);
}

请参阅此处 你会发现:


"每种语言的扩展点通常比Windows运行时界面更容易使用。 JavaScript有
WinJS.Promise
then / done 语法。 .NET具有
AsTask
扩展方法,一旦 IAsyncOperationWithProgress< TResult,TProgress>  转换为
任务< TResult> ,更容易获得结果,取消,完成后收到通知,"


如果您发现可以毫无问题地运行官方样本,那么请检查您的代码我提到的内容。似乎这是你的问题。


祝你好运,


Barry



I am developing a UWP app where I need to scan images using the Windows.Devices.Scanners.ImageScanner class. Basically, these are the steps:

  1. Obtain a device ID using DevicePicker class
  2. Construct an ImageScanner using ImageScanner.FromIdAsync(...)
  3. Call ScanFilesToFolderAsync(...) to scan an image and save it to the temporary folder (ApplicationData.Current.TemporaryFolder). This returns an ImageScannerScanResult.
  4. Use ImageScannerScanResult.ScannedFiles to get the image file

Steps 1 - 3 work correctly, then an InvalidCastException occurs in step 4:

Unable to cast object of type 'System.__ComObject' to type 'System.Runtime.InteropServices.WindowsRuntime.IIterable`1[Windows.Storage.StorageFile]'.

解决方案

Hello SvenEV,

The cast error seems tell us that the problem is not a known issue.

Can you try the official sample: https://code.msdn.microsoft.com/windowsapps/Scan-Runtime-API-Sample-5703b7a2/sourcecode?fileId=86722&pathId=1503360506 and test whether you can run it without problem first?

I don't have a device to help you test this issue, but please check the return result of ScanFilesToFolderAsync | scanFilesToFolderAsync method. It's IAsyncOperationWithProgress<ImageScannerScanResult, UInt32>

So in our official sample you will see the following code:

https://code.msdn.microsoft.com/windowsapps/Scan-Runtime-API-Sample-5703b7a2/sourcecode?fileId=86722&pathId=1503360506

  var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Default, folder).AsTask(cancellationToken.Token, progress); 
                    ModelDataContext.ScenarioRunning = false; 
                    if (result.ScannedFiles.Count > 0) 
                    { 
                        Utils.DisplayImageAndScanCompleteMessage(result.ScannedFiles, DisplayImage); 
                        if (result.ScannedFiles.Count > 1) 
                        { 
                            Utils.UpdateFileListData(result.ScannedFiles, ModelDataContext); 
                        } 
                    } 
                    else 
                    { 
                        rootPage.NotifyUser("There were no files scanned.", NotifyType.ErrorMessage); 
                    } 

See here you will find this:

"Each of the languages has extension points that are generally easier to use than the Windows Runtime interface. JavaScript has WinJS.Promise, and the then/done syntax. .NET has the AsTask extension method, and once the IAsyncOperationWithProgress<TResult,TProgress>  is converted to a Task<TResult>, it's easier to get the result, cancel, get notification on completion, "

If you find that you can run official sample without problem, then please check your code about what I mentioned. Seems this is the problem of your issue.

Best regards,

Barry


这篇关于[UWP]错误报告:ImageScannerScanResult.ScannedFiles抛出InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆