MediaLibrary图片的InvalidOperationException

Phi*_*ipp 5 c# asynchronous invalidoperationexception windows-phone-8

我有一个Windows手机应用程序,有时得到InvalidOperationExceptions但不知道为什么以及如何避免它们.错误报告中的问题函数是Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].get_Item,我得到这个堆栈跟踪

"Frame    Image                             Function                                                                                                                                    Offset        
0        Microsoft.Xna.Framework.ni.dll    Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].get_Item                                                 0x0003e4d8    
1        Microsoft.Xna.Framework.ni.dll    Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].System.Collections.IEnumerator.get_Current               0x00000006    
2        Microsoft.Xna.Framework.ni.dll    Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].System.Collections.Generic.IEnumerator_T_.get_Current    0x0000001c    
3        MapLense.ni.DLL                   MapLense.Helper.PictureMapping.Add                                                                                                          0x000000a8    
4        MapLense.ni.DLL                   MapLense.Helper.PictureMapping+_GetPicture_d__b.MoveNext                                                                                    0x000000f6    
5        mscorlib.ni.dll                   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess                                                                              0x00216c46    
6        mscorlib.ni.dll                   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification                                                         0x0000003a    
7        mscorlib.ni.dll                   System.Runtime.CompilerServices.TaskAwaiter_1[[System.__Canon,_mscorlib]].GetResult                                                         0x0000001c    
8        MapLense.ni.DLL                   MapLense.Helper.Map+_AddPictureToMap_d__17.MoveNext                                                                                         0x00000118    
9        mscorlib.ni.dll                   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess                                                                              0x00216c46    
10       mscorlib.ni.dll                   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification                                                         0x0000003a    
11       mscorlib.ni.dll                   System.Runtime.CompilerServices.TaskAwaiter_1[[System.__Canon,_mscorlib]].GetResult                                                         0x0000001c    
12       MapLense.ni.DLL                   MapLense.MainPage+_ViewModelOnPropertyChanged_d__1e.MoveNext                                                                                0x00000204    
13       mscorlib.ni.dll                   System.Runtime.CompilerServices.AsyncMethodBuilderCore._ThrowAsync_b__0                                                                     0x00000036"
Run Code Online (Sandbox Code Playgroud)

我还尝试在代码块周围添加一个try-catch块,但没有结果

public static bool Add(DBPicture dbpicture)
{
    if (Pictures.ContainsKey(dbpicture.UniqueID))
        return true;

    var root = new MediaLibrary().RootPictureAlbum;

    foreach (var album in root.Albums)
    {
        if (album.Name != AppResources.CameraRollAlbumName) continue;

        for (var i = 0; i < album.Pictures.Count; i++)
        {
            try
            {
                var picture = album.Pictures[i];
                if (picture.Name == dbpicture.UniqueID)
                {
                    Pictures.Add(picture.Name, picture);
                    DBPictures.Add(picture.Name, dbpicture);
                    return true;
                }
            }
            catch (System.Exception e)
            {
#if DEBUG
                Logger.WriteLine("PictureMapping.Add(DBPicture)");
                Logger.WriteLine(e);
#endif
            }
        }
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的任何建议

Ped*_*Kid 1

不是一个真正的答案,但如果错误是第一个错误foreachtry第二个错误是错误,那么它不会捕获异常,将其放在try外面

try
{
    var root = new MediaLibrary().RootPictureAlbum;
Run Code Online (Sandbox Code Playgroud)