mauiwindows-community-toolkit

MAUI Command on menubar will only navigate the first time to the page


I have an apps settings page in the menubar and I also have a button on the main page. If the settings menuitem is clicked first then it goes to the Settings command otherwise nothing happens. The button always goes to the Settings command. It doesn't matter which control is clicked first. It will go to settings, however after the first time going to settings by either control the menubar settings will not execute the settings command as in the break point in the settings command doesn't get hit.

Here is the menuitem xaml:

<ContentPage.MenuBarItems>
    <MenuBarItem Text="File">
        <MenuFlyoutItem Text="Save" />
        <MenuFlyoutItem Command="{x:Binding SettingsCommand}" Text="Setting" />

Here is the button control:

<Button x:Name="btnSettings"
        Grid.Row="3"
        Grid.Column="1"
        Margin="5"
        Command="{x:Binding SettingsCommand}"
        Text="Settings" />

Here is the command (Community Toolkit)

[RelayCommand]
public void Settings()
{
    Shell.Current.GoToAsync(nameof(SettingsPage));
}

Here is the close command in the setting viewmode.

[RelayCommand]
public void Close()
{
    Shell.Current.GoToAsync("..");
}

Solution

  • This is an known open issue, #13848. The workaround is to rebind all the MenuBarItems.

    protected override void OnAppearing()
    {
        base.OnAppearing();
    
        foreach (var item in MenuBarItems)
        {
            item.BindingContext = BindingContext;
        }
    }
    

    Kudos to Vlad for that.