dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.93k stars 1.7k forks source link

Page changing using menu bar stops command in menu bar from working #13848

Open HimanshuBansal112 opened 1 year ago

HimanshuBansal112 commented 1 year ago

Description

Since MenuBarItem did not had command binding option, I chose MenuFlyoutItem. But when doing navigation in it, it stops command in menu bar from working. It doesn't crash but it stops working.

Steps to Reproduce

Add to MainPage.xaml:

<ContentPage.MenuBarItems>
      <MenuBarItem Text="Pages">
            <MenuFlyoutItem Text="Page1"
                            Command="{Binding TapCommand}"/>
        </MenuBarItem>
</ContentPage.MenuBarItems>

Add to MainViewModel.cs:

        [RelayCommand]
        async Task Tap()
        {
            await Shell.Current.GoToAsync(nameof(Page1));
        }

Add to Page1.xaml

<ContentPage.MenuBarItems>
        <MenuBarItem Text="Options">
            <MenuFlyoutItem Text="GoBack"
                            Command="{Binding GoBackCommand}"/>
            <MenuFlyoutItem Text="Exit"
                            Command="{Binding ExitCommand}"/>
        </MenuBarItem>
    </ContentPage.MenuBarItems>

Add to Page1ViewModel.cs

        [RelayCommand]
        public static void Exit()
        {
            System.Environment.Exit(0);
        }

        [RelayCommand]
        async Task GoBack()
        {
            await Shell.Current.GoToAsync("..");
        }

As usual add to AppShell.xaml.cs

Routing.RegisterRoute(nameof(Page1), typeof(Page1));

and add to MauiProgram.cs

                builder.Services.AddSingleton<MainPage>();
        builder.Services.AddSingleton<MainViewModel>();

        builder.Services.AddTransient<Page1>();
        builder.Services.AddTransient<Page1>();

Link to public reproduction project repository

https://github.com/HimanshuBansal112/NetMauiIssue

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

I was not able test on other platforms

Affected platform versions

Windows 11(Build 22621.1344)

Did you find any workaround?

No

Relevant log output

N/A
ghost commented 1 year ago

Hi @HimanshuBansal112. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

HimanshuBansal112 commented 1 year ago

I tested in .net 8, it does not work there as well. I have made a repo at https://github.com/HimanshuBansal112/NetMauiIssue It also has issue after we go to any page from menu bar then come back and then try to go again, it does not work.

ghost commented 1 year ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

vrishe commented 1 year ago

Seems like the flyout items' binding context is getting lost during the navigation process. For me I have 'fixed' the issue like this:

protected override void OnAppearing()
{
    base.OnAppearing();

    foreach (var item in MenuBarItems)
    {
        item.BindingContext = BindingContext;
    }
}
borup3 commented 1 year ago

I have the same issue and @vrishe's solution fixes it. If we're not supposed to do things like this (since this is "under consideration"?) how are we supposed to do it??

ooikengsiang commented 12 months ago

I have the same issue and @vrishe's solution fixes it. If we're not supposed to do things like this (since this is "under consideration"?) how are we supposed to do it??

What vrishe proposed is a workaround, not a fix. Fix mean it should have work correctly as intended, no work around needed.

MaxMa04 commented 4 months ago

Seems like the flyout items' binding context is getting lost during the navigation process. For me I have 'fixed' the issue like this:

protected override void OnAppearing()
{
    base.OnAppearing();

    foreach (var item in MenuBarItems)
    {
        item.BindingContext = BindingContext;
    }
}

Works like charm thank you Brother