I have a Maui app, with an item collection view and an item detail view. When an item is tapped in the collection view, I'd like to navigate to the detail view. I use Maui Shell navigation. The code comes from a Xamarin app, where it used to work. The route is registered in AppShell.xaml
In the tap event handler on the collection page code behind
async void OnItemTapped(ItemViewModel itemVM)
{
string route =
$"//{nameof(ItemPage)}?{nameof(ItemPage.Id)}={itemVM.Id}";
await Shell.Current.GoToAsync(route);
}
In debugging, I can verify that the contents of variable route are as expected.
Details page code behind (redacted to relevant bits):
[XamlCompilation(XamlCompilationOptions.Compile)]
[QueryProperty(nameof(Id), nameof(Id))]
public partial class ItemPage : ContentPage, IDisposable
{
/// <summary>Navigation property to pass Id value.</summary>
public string Id { get; set; }
public TablePartyPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
// Id is populated by navigation.
string id = TablePartyId.FromUrlQueryValue(Id); /* Problem: Id is null here */
var viewModel = new ItemViewModel(
...
);
BindingContext = viewModel;
}
}
On executing GotoAsync() the ItemPage constructor, then ItemPage OnAppearing() is executed, however, the navigation property is not populated.
What am I missing?
Environment is:
It seems like it was a navigation issue, mentioned in this related question: How to register a non-shell-visible navigation route in Maui Shell?
Fixed the non-shell navigation as suggested in the first comment there and navigation property was populated.
Thank you for all your suggestions!