0

下面的代码会抛出 invalidOperationException,但前提是调试器已分离。该代码以 7.1 为目标,用户不时报告问题。这是我第一次可以在 windows phone 8 设备上重现此问题,但仅在我分离调试器时。附加调试器后,代码运行良好。这个问题不是一致的再现。在分析中,我看到一小部分用户面临这个问题。知道这里发生了什么吗?

       PictureAlbum localRoll = media.GetCameraRoll();
       foreach (Picture pic in localRoll.Pictures) // exception here
       {

       }


        public PictureAlbum GetCameraRoll()
        {
        // Work around for known bug in the media framework.  Hits the static constructors
        // so the user does not need to go to the picture hub first.
        MediaPlayer.Queue.ToString();

        MediaLibrary ml = null;
        PictureAlbum cameraRoll = null;

        foreach (MediaSource source in MediaSource.GetAvailableMediaSources())
        {
            if (source.MediaSourceType == MediaSourceType.LocalDevice)
            {
                ml = new MediaLibrary(source);

                PictureAlbumCollection allAlbums = ml.RootPictureAlbum.Albums;

                foreach (PictureAlbum album in allAlbums)
                {
                    if (album.Name == "Camera Roll")
                    {
                        cameraRoll = album;
                        return cameraRoll;
                    }
                }
            }
        }

        return null;
    }

System.InvalidOperationException:发生意外错误。在 Microsoft.Xna.Framework.Media.MediaLibraryEnumerator'1.System.Collection.Ienumerator'1.get_Item(Int32 索引)

4

2 回答 2

2

很奇怪但有效。首先在图片上调用 OrderBy 不会引发异常。

                var a = localRoll.Pictures;

                foreach (Picture pic in a.OrderBy(x=>x.Date))
                {

                }
于 2013-11-14T10:35:58.283 回答
0

请检查是否localRoll并且因为某些手机可能有 0 张图片localRoll.Pictures != null并且localRoll.Pictures.Count > 0代码正在尝试遍历null集合。

   PictureAlbum localRoll = media.GetCameraRoll();
   if(localRoll != null)
   {
      if(localRoll.Pictures != null)
      {
          if(localRoll.Pictures.Count > 0)
          {
             foreach (Picture pic in localRoll.Pictures)
             {

             }
          }
      }
   }
于 2013-11-13T07:29:51.157 回答