androidmauiapp-shell

How pass parameter when navigate to another page (Shell)


I've just started trying to use MAUI in Visual studio to develop a multiplatform app. I have managed to use shell navigation and this works well using the flyout.

What I want to do is have a button click which navigates to another page passing a varialbe with it.

What does work (kind of) is this:

await Navigation.PushModalAsync(new ResultListView(ordnum));

This does navigate to the page and passes the var ordnum as I want, however it breaks out of the shell navigation (I loose the flyout etc).

What I can't figure out is how to do it within Shell

await Shell.Current.GoToAsync

seems to be what I want, but when I use routings I can't specify the variable in the same way?

This seems such a basic question I'm sure it's been asked before, but I honestly can't find it. I've spent all morning searching for an answer! The answers I did find about passing variables through XAML were very confusing.

Thanks

Andrew


Solution

  • Lest supose you want to send an object PRODUCT

    private Product productToSend;
    private Product anotherProductToSend;
    
    await Shell.Current.GoToAsync($"{nameof(YourPageHere)}?",
                    new Dictionary<string, object>
                    {
                        ["Object1"] = productToSend,
                        ["Object2"] = anotherProductToSend
                    });
    

    Then in your pageViewModel:

    [QueryProperty("Object1", "Object1")]
    [QueryProperty("Object2", "Object2")]
    public partial class YourPageHereViewModel : ObservableObject
    {
        [ObservableProperty]
        Product Object1;
    
        [ObservableProperty]
        Product Object2;
    }