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

问题描述 投票:0回答:1

我试图通过 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;

viewmodel 通过使用BindingContext 链接到页面。 当我改为传递包含 ImageSource 的 TempPhotoPoint 对象时,一切正常。 怎么可能只传递 ImageSource 对象?

shell maui imagesource
1个回答
0
投票

我不知道为什么这不起作用,但您可以通过实施方法

ApplyQueryAttributes
来解决它,如 Process navigation data using a single method 中所示。

  • 务必将

    : IQueryAttributable
    添加到您的视图模型类声明中,以便 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 ...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.