我正在使用Xamarin在便携式类库中构建Windows手机应用程序 . 我从Windows Phone相机胶卷获得了图像,我将List传回PCL,并在我的视图中将图像分配给ImageSource .
Windows Phone Get Images:
foreach (var image in CameraRollPictures)
{
Image img = new Image();
img.Source = ImageSource.FromStream(() => image.GetImage());
images.Add(img);
}
return images;
PCL method:
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();
}));
View:
<Image Source="{Binding ImageSrc}"/>
The Exception:
{System.UnauthorizedAccessException:无效的跨线程访问 . 在位于Xamarin.Forms.Platform.WinPhone的System.Windows.Media.Imaging.BitmapImage..ctor()的System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex,IntPtr constructDO)的MS.Internal.XcpImports.CheckThread()处 . StreamImagesourceHandler.d__0.MoveNext()---抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处(任务)在Xamarin.Forms.Platform.WinPhone.ImageRenderer.d__0.MoveNext()的System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()的任务)---从抛出异常的上一个位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__3(Object state)}
UPDATE
如果我从资源加载图片,那么它工作正常 .
App.Locator.TaggingPageVM.ImageSrc = ImageSource.FromFile("50175950-tulips-microsofts.jpg");
在Windows手机上创建的图像并将这些图像传回我的PCL一定是错误的
1 回答
我正在使用Xamarin:
修复了跨线程错误