shell 将ImageSource作为参数传递给另一个页面

llew8vvj  于 2023-04-21  发布在  Shell
关注(0)|答案(2)|浏览(113)

我尝试通过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对象呢?

zmeyuzjn

zmeyuzjn1#

我不知道为什么这不起作用,但是您可以通过实现方法ApplyQueryAttributes来解决它,如使用单个方法处理导航数据中所示。

  • 确保将: IQueryAttributable添加到viewmodel类声明中,以便Maui知道使用该方法。
  • 在方法中,指定转换:
... class MyViewModel : IQueryAttributable
{
    ...

    public void ApplyQueryAttributes(IDictionary<string, object> query)
    {
        TempImageSource = query["TempImageSource"] as ImageSource;
        OnPropertyChanged(nameof(TempImageSource));

        ... repeat for all other query parameters ...
    }
}
vql8enpb

vql8enpb2#

两个异常的原因是一样的,我之前把原始ImageSource绑定到一个Image,通过绑定,流被关闭了,当我尝试在FullScreenImagePage中重新绑定时关闭的流导致了异常,当我之前没有绑定的时候传入的ImageSource可以用我在第一篇文章中描述的方式绑定。我将尝试将StreamImageSource复制到MemoryStream中并使用它。当我找到解决方案时,我会在这里发布。

相关问题