Reputation: 35
I'm trying to navigate to the same one page with different query parameters.
AppShell.XAML:
<MenuItem Text="A"
Command="{Binding NavCommand}"
CommandParameter="A"/>
<MenuItem Text="B"
Command="{Binding NavCommand}"
CommandParameter="B"/>
Code behind:
public Command NavCommand { get; set; }
public AppShell()
{
InitializeComponent();
NavCommand = new Command<string>(OnMenuItemClicked);
Routing.RegisterRoute(nameof(ListsPage), typeof(ListsPage));
BindingContext = this;
}
public async void OnMenuItemClicked(string MyQuery)
{
await Shell.Current.GoToAsync($"//{nameof(ListsPage)}?{nameof(ListsViewModel.Group)}={MyQuery}");
Shell.Current.FlyoutIsPresented = false;
}
It works, if I use await Shell.Current.GoToAsync($"{nameof(ListsPage)}?{nameof(ListsViewModel.Group)}={MyQuery}");
, but it makes the page second level and hides the hambugber icon.
If I useawait Shell.Current.GoToAsync($"//{nameof(ListsPage)}?{nameof(ListsViewModel.Group)}={MyQuery}");
it goes to the page and hamburger icon still presented, but it's not taking MyQuery value.
I have to navigate to another page, then I can click to the menu item and navigate with given parameter. But after that, it's the same - Query parameter still remain and won't change. Again, have to navigate to another page, then I can click to the menu item and navigate with given parameter.
Upvotes: 0
Views: 225
Reputation: 10078
You need to receive the passed data via using QueryProperty
to get the MyQuery
in next page ListsPage
, and you should add a default constructor for the ListsPage
,for example:
[QueryProperty("QueryID", "id")]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ListsPage : ContentPage
{
private string _typeId;
public ListsPage()
{
InitializeComponent();
BindingContext = new ListsPageViewModel();
}
public string QueryID
{
get => _typeId;
set => MyLabel.Text = value;
}
}
For more details, you can check the links below:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#pass-data
Upvotes: 1