【发布时间】:2014-08-13 14:25:49
【问题描述】:
我正在使用可移植类库中的 Xamarin 构建 Windows 手机应用程序。我从 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: 无效的跨线程访问。
在 MS.Internal.XcpImports.CheckThread() 在 System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex,IntPtr constructDO) 在 System.Windows.Media.Imaging.BitmapImage..ctor()
在 Xamarin.Forms.Platform.WinPhone.StreamImagesourceHandler.d__0.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 Xamarin.Forms.Platform.WinPhone.ImageRenderer.d__0.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__3(对象 州)}
更新
如果我从资源加载图片,那么它可以正常工作。
App.Locator.TaggingPageVM.ImageSrc = ImageSource.FromFile("50175950-tulips-microsofts.jpg");
在 windows phone 上创建的图像并将这些图像传回我的 PCL 一定是错误
【问题讨论】:
-
PageOp.Navigate 之后有代码,不确定这是个好主意。
-
PageOP.Navigate 基本上调用
Navigation.PushAsync(Page)然后App.Locator.TaggingPageVM找到与此页面关联的VM 并设置ImageSrc属性 -
您是否尝试过将该行包装在调用程序中? Deployment.Current.Dispatcher.BeginInvoke(() => { App.Locator.TaggingPageVM.ImageSrc = sender.Result[0].Source; });
-
我正在使用 Xamarin 但我使用了 Device.BeginInvokeOnMainThread(() => { App.Locator.TaggingPageVM.ImageSrc = sender.Result[0].Source; });这是一种类似的方法并且有效
标签: c# multithreading windows-phone-8 xamarin.ios xamarin