我尝试通过shell导航将ImageSource对象传递到另一个页面。
当我打电话
await Shell.Current.GoToAsync($"{nameof(FullScreenImagePage)}?",
new Dictionary<string, object>
{
["TempImageSource"] = TempPhotoPoint.ExamplePicture
});
我得到以下异常:System.InvalidCastException:'对象必须实现IConvertible。'
ExamplePicture的类型为ImageSource。
FullScreenImagePage有一个视图模型,其中包含
[QueryProperty(nameof(TempImageSource), nameof(TempImageSource))]
和
[ObservableProperty] ImageSource tempImageSource;
视图模型通过BindingContext链接到页面。当我传递TempPhotoPoint对象时,它包含一个ImageSource,一切都很好。怎么可能只传递ImageSource对象呢?
2条答案
按热度按时间zmeyuzjn1#
我不知道为什么这不起作用,但是您可以通过实现方法
ApplyQueryAttributes
来解决它,如使用单个方法处理导航数据中所示。: IQueryAttributable
添加到viewmodel类声明中,以便Maui知道使用该方法。vql8enpb2#
两个异常的原因是一样的,我之前把原始ImageSource绑定到一个Image,通过绑定,流被关闭了,当我尝试在FullScreenImagePage中重新绑定时关闭的流导致了异常,当我之前没有绑定的时候传入的ImageSource可以用我在第一篇文章中描述的方式绑定。我将尝试将StreamImageSource复制到MemoryStream中并使用它。当我找到解决方案时,我会在这里发布。