简体   繁体   English

uwp NeighbourFilesQuery 有时会给出 System.OverflowException

[英]uwp NeighbourFilesQuery sometimes gives System.OverflowException

I use File​Activated​Event​Args args to do the file association in my video player app.我使用File Activated Event Args args在我的视频播放器应用程序中进行文件关联。 So I when user double clicks a video file it opens and plays the file in my app, also it gets all the neighbouring files, so I can add them to playlist as well.因此,当用户双击一个视频文件时,它会打开并在我的应用程序中播放该文件,同时它还会获取所有相邻的文件,因此我也可以将它们添加到播放列表中。 I use following code for this purpose.为此,我使用以下代码。

var file=args.Files[0]; 
StorageFileQueryResult neighboringFilesQuery=args.NeighboringFilesQuery;
var startingIndexunit = await neighboringFilesQuery.FindStartIndexAsync(file);
int startingIndex = Convert.ToInt32(startingIndexunit); //this is where exception occurs

not always but sometimes when I open a file I get a System.OverFlowException because the code tries to enter a very large garbage number into int32 which causes the exception.并非总是如此,但有时当我打开一个文件时,我会收到System.OverFlowException,因为代码试图将一个非常大的垃圾数输入int32 ,这会导致异常。

After further investigation I have discovered that usually when I double click a file and get neighbor files, I get all the files in NeighborFilesQuery (including the 1 file I clicked to open) so I can just find its index so I can set the start index of my playlist in my app, and play the clicked file at correct position.经过进一步调查,我发现通常当我双击一个文件并获取邻居文件时,我会获取NeighborFilesQuery 中的所有文件(包括我单击打开的 1 个文件),因此我可以找到它的索引,以便我可以设置起始索引在我的应用程序中播放我的播放列表,并在正确的位置播放单击的文件。

But in some other cases for example when I open a .flv or some a .rm file, I get all neighbor files in the neighborfilesQuery but I don't get the file I clicked, so when the code tries to get the index of that file, that file doesn't exist in that list hence I get a garbage index.但是在其他一些情况下,例如当我打开一个.flv或一些.rm文件时,我得到了neighborfilesQuery 中的所有邻居文件,但我没有得到我点击的文件,所以当代码试图获取该文件的索引时文件,该文件不存在于该列表中,因此我得到一个垃圾索引。

So why is this api so inconsistent?那么为什么这个api如此不一致呢? Sometimes it includes the clicked file in query files list, and sometimes it doesn't?有时它会在查询文件列表中包含单击的文件,有时它不会?

Note please note that I am only talking about a single file clicked scenario here and not about multiple files opened together (because in that case the files query is supposed to be empty and that is a different scenario).注意:请注意,我只谈论一个文件这里,而不是点击场景约一起打开(因为在这种情况下,文件查询应该是空的,这是一个不同的场景)的多个文件。

According to the documentation, when FindStartIndexAsync, you should expect to see uint.MaxValue.根据文档,当 FindStartIndexAsync 时,您应该期望看到 uint.MaxValue。 https://docs.microsoft.com/en-us/uwp/api/windows.storage.search.storagefilequeryresult.findstartindexasync https://docs.microsoft.com/en-us/uwp/api/windows.storage.search.storagefilequeryresult.findstartindexasync

If we check the NeighboringFilesQuery documentation here: https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.activation.fileactivatedeventargs.neighboringfilesquery , we see that it's not available for all invocations.如果我们在此处查看 NeighboringFilesQuery 文档: https : //docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.activation.fileactivatedeventargs.neighboringfilesquery ,我们会发现它不适用于所有调用。 We'll also notice that none of the mp4 files show in the NeighboringFileQuery, so when we activate by clicking on the mp4, the mp4 is still not in the NeighboringFileQuery list.我们还会注意到 NeighboringFileQuery 中没有显示任何 mp4 文件,因此当我们通过单击 mp4 激活时,mp4 仍然不在 NeighboringFileQuery 列表中。 When you call to get the index, it returns uint.MaxValue since the requested file is not in the query.当您调用以获取索引时,它返回 uint.MaxValue,因为请求的文件不在查询中。 The exception occurs due to an overflow when trying to convert this to Int32.尝试将其转换为 Int32 时由于溢出而发生异常。

For this case, you'll want to check against uint.MaxValue prior to casting/converting to Int32.对于这种情况,您需要在转换/转换为 Int32 之前检查 uint.MaxValue。 You should also catch any exception that might occur, since you'll get the overflow for any value greater than int.MaxValue.您还应该捕获可能发生的任何异常,因为您将获得大于 int.MaxValue 的任何值的溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM