question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MenuFlyoutItem command binding not working in submenu items

See original GitHub issue

Description

MenuFlyoutItem command binding does not appear to work in the case below. This is similar to https://github.com/dotnet/maui/issues/5382, but that issue is closed.

A workaround at https://github.com/dotnet/maui/issues/5382 is mentioned and works as shown below. Should the NON WORKING EXAMPLE work without the 5382 workaround?

For example, I would expect the DoSomethingCommand to run below, but it only works if I modify the code below to use the workaround.

NON WORKING EXAMPLE `<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodels="clr-namespace:MauiApp1.ViewModel" x:Class="MauiApp1.MainPage" x:DataType="viewmodels:MainPageViewModel" x:Name="thisMenuBarWorkaround">

<ContentPage.BindingContext>
    <viewmodels:MainPageViewModel/>
</ContentPage.BindingContext>

<ContentPage.MenuBarItems>
    <MenuBarItem Text="File">
        <MenuFlyoutSubItem Text="Open">
            <MenuFlyoutItem Text="Open Do Something..."
                            Command="{Binding DoSomethingCommand}"/>
            
        </MenuFlyoutSubItem>
    </MenuBarItem>
</ContentPage.MenuBarItems>

</ContentPage>`

WORKING EXAMPLE USING WORKAROUND `<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodels="clr-namespace:MauiApp1.ViewModel" x:Class="MauiApp1.MainPage" x:DataType="viewmodels:MainPageViewModel" x:Name="thisMenuBarWorkaround">

<ContentPage.BindingContext>
    <viewmodels:MainPageViewModel/>
</ContentPage.BindingContext>

<ContentPage.MenuBarItems>
    <!--https://github.com/dotnet/maui/issues/5382 workaround-->
    <MenuBarItem Text="File"
                 BindingContext="{Binding Path=BindingContext,
                                          Source={x:Reference thisMenuBarWorkaround}}">
        <MenuFlyoutSubItem Text="Open">
            <MenuFlyoutItem Text="Open Do Something..."
                            Command="{Binding DoSomethingCommand}"/>
            
        </MenuFlyoutSubItem>
    </MenuBarItem>
</ContentPage.MenuBarItems>

</ContentPage>`

VIEWMODEL

using CommunityToolkit.Mvvm.Input;

namespace MauiApp1.ViewModel;

public partial class MainPageViewModel
{
    [RelayCommand]
    public static void DoSomething()
    {
        // This point is never hit without workaround
        return;
    }

Steps to Reproduce

  1. Load github example below and run solution.
  2. Note that the menu item will not reach the ViewModel command

Link to public reproduction project repository

https://github.com/jeff922/MauiMenuTest

Version with bug

7.0 Release Candidate 2

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

.NET 7.0.100-rc.2.22477.23 and Microsoft Visual Studio Community 2022 (64-bit) - Preview Version 17.4.0 Preview 3.0

Did you find any workaround?

Workaround mentioned in https://github.com/jeff922/MauiMenuTest.

Relevant log output

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jeff922commented, Oct 20, 2022

@PureWeen Thanks. I updated VS to Preview 4 and this fixed the issue

0reactions
PureWeencommented, Oct 20, 2022

Thank you for following up @jeff922 !!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Command bound with MenuFlyoutItem is not getting ...
Defined a Menu structure in a Content page and added Command to the MenuFlyoutItem, but the Command is not getting executed when invoked....
Read more >
Command Binding not working within Flyout or FlyoutMenu
If you set the MenuFlyoutItem Command property using x:Bind instead of Binding, this will work, so long as the MenuFlyout has an x:Name....
Read more >
NET MAUI Shell flyout
Learn how to customize and control a .NET MAUI flyout, which is the optional root menu for a .NET MAUI Shell app.
Read more >
.NET Maui Flyout Menus not working - Microsoft Q&A
My .NET Maui FlyoutMenus are not working with Android Emulator. The code works correctly as expected using Windows.
Read more >
Context Flyout in WinUI TreeView control
The TreeView allows to show context flyout using custom commands when built-in commands does not meet your requirement. For an example, custom command...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found