4

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

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
Andrew Taylor
  • 145
  • 13
  • 2
    https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation#pass-data – Jason Aug 11 '22 at 11:25

1 Answers1

5

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;
}
Leandro Toloza
  • 1,655
  • 1
  • 6
  • 24
  • Hi, Thanks for the tips, I'm still struggling to get this to work. Do I have to use a View Model? Currently I do not have any view models, I just have 2 pages I want to navigate to, this works fine: await Navigation.PushModalAsync(new ResultListView(ordnum)); but breaks out of the shell navigation. – Andrew Taylor Sep 02 '22 at 06:56
  • Is this the only way to do this? Do I have to have a page ViewModel? My app is becoming cluttered with ViewModels and all I want is to get at the data passed – djack109 Sep 15 '22 at 20:11
  • @AndrewTaylor viewmodel it's the easy way to do it, if not you can pass data via contructor when you call your page! – Leandro Toloza Sep 15 '22 at 21:13
  • @djack109 i will make you an example to pass data without viewmodel, give me some minutes plz – Leandro Toloza Sep 15 '22 at 21:14
  • @LeandroToloza did you have an example of how to do this without using a view model? My app is a very basic idea and I’m stuck on the most simple part! – Andrew Taylor Feb 07 '23 at 19:14
  • I resorted to using viewmodels, which seems to overcomplicate everything for such a simple function. But now, I can bind the view model to the view. So I can bind a string called count to a label in XAML, this works well.... but how to I access this parameter in viewmodel without referencing the label? I can do this: Debug.WriteLine(MainLabel.Text) and have it bound to the string count, but I can't do this Debug.WriteLine(Count). I'm very confused! – Andrew Taylor Feb 09 '23 at 12:01