繁体   English   中英

尝试在Windows Phone上显示相机胶卷中的图像时出现跨线程异常

[英]Cross Thread Exception when trying to display Images from Camera Roll on Windows Phone

我正在使用可移植类库中的Xamarin构建Windows Phone应用程序。 我已经从Windows Phone相机胶卷中获取了图像,并且正在将列表传递回PCL,并在视图中将图像分配给ImageSource。

Windows Phone获取图像:

foreach (var image in CameraRollPictures)
{
    Image img = new Image();
    img.Source = ImageSource.FromStream(() => image.GetImage());
    images.Add(img);
}           
return images;

PCL方法:

    private RelayCommand _importPhoto;
    public RelayCommand ImportPhoto
    {
        get
        {
            return _importPhoto
                ?? (_importPhoto = new RelayCommand(
                                      () =>
                                      {
                                          IOperations op = DependencyService.Get<IOperations>();                                            
                                          Task<List<Image>> t = new Task<List<Image>>(() =>
                                          {
                                              return op.ImportPhoto();

                                          });
                                          t.ContinueWith((sender) =>
                                              {
                                                  PageOp.Navigate(new TaggingPage());
                                                  if (sender.Result.Count != 0)
                                                  {
                                                      try
                                                      {
                                                          App.Locator.TaggingPageVM.ImageSrc = sender.Result[0].Source;
                                                      }
                                                      catch (Exception ex)
                                                      {

                                                      }
                                                  }
                                              }, TaskScheduler.FromCurrentSynchronizationContext());
                                          t.Start();
                                      }));

视图:

<Image Source="{Binding ImageSrc}"/>

例外:

{System.UnauthorizedAccessException:无效的跨线程访问。
在System.Windows.DependencyObject..ctor(在MS.Internal.XcpImports.CheckThread()在System.Windows.Media.Imaging.BitmapImage..ctor()在UInt32 nativeTypeIndex,IntPtr ConstructDO)
在Xamarin.Forms.Platform.WinPhone.StreamImagesourceHandler.d__0.MoveNext()处-从上次引发异常的位置开始的堆栈跟踪-在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()位于Xamarin.Forms.Platform.WinPhone.ImageRenderer.d__0.MoveNext()上的Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)在System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__3(对象状态)}处引发异常的位置

UPDATE

如果我从资源中加载图片,则可以正常工作。

App.Locator.TaggingPageVM.ImageSrc = ImageSource.FromFile("50175950-tulips-microsofts.jpg");

处理在Windows Phone上创建的图像并将这些图像传回我的PCL一定是错误的

我正在使用Xamarin,因此:

Device.BeginInvokeOnMainThread(() => { App.Locator.TaggingPageVM.ImageSrc = sender.Result[0].Source; }); 

修复了跨线程错误

暂无
暂无

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

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