XF-Material-Library icon indicating copy to clipboard operation
XF-Material-Library copied to clipboard

MaterialMenuButton - assigned Command is not being called on menu selection

Open lwg-jmarciniak opened this issue 5 years ago • 1 comments

🐛 Bug Report

When the Command property is bound to an ICommand that exists in the ViewModel, the associated function is not being executed when a menu item is selected.

Expected behavior

If the Command property has a binding (i.e. ... Command="{Binding MenuCommand}" ... the associated view model command should be executed when the user selects a menu item.

Reproduction steps

Using the sample project in this repository, this is very easy to reproduce:

  1. Add the following to the MaterialMenuButton markup in Views/MaterialMenuButtonView.xaml
<material:MaterialMenuButton  Image="icon_more.png" TintColor="Black" ButtonType="Text" CornerRadius="24" Choices="{Binding Actions}" Command="{Binding MenuCommand}" CommandParameter="CommandTest" MenuSelected="MenuSelected" />
  1. Change Views/MaterialMenuButtonView.xaml.cs to the following
using MaterialMvvmSample.ViewModels;
using Xamarin.Forms;
using XF.Material.Forms.UI;
using XF.Material.Forms.UI.Dialogs;

namespace MaterialMvvmSample.Views
{
    public partial class MaterialMenuButtonView : ContentPage
    {
        public MaterialMenuButtonView()
        {
            InitializeComponent();
            BindingContext = new MaterialMenuButtonViewModel();
        }

        private void MaterialMenuButton_MenuSelected(object sender, MenuSelectedEventArgs e)
        {
            MaterialDialog.Instance.AlertAsync("MenuSelected");
        }
    }
}
  1. Change ViewModels/MaterialMenuButtonViewModel.cs to the following
using System.Windows.Input;
using Xamarin.Forms;
using XF.Material.Forms.Models;
using XF.Material.Forms.UI.Dialogs;

namespace MaterialMvvmSample.ViewModels
{
    public class MaterialMenuButtonViewModel : BaseViewModel
    {
        public MaterialMenuItem[] Actions => new MaterialMenuItem[]
        {
            new MaterialMenuItem
            {
                Text = "Edit"
            },
            new MaterialMenuItem
            {
                Text = "Delete"
            }
        };

        public ICommand MenuCommand = new Command(
            execute: (arg) =>
            {
                MaterialDialog.Instance.AlertAsync("MenuCommand");
            },
            canExecute: (x) =>
            {
                bool? retval = MaterialDialog.Instance.ConfirmAsync(message: "Allow Menu?", confirmingText: "Yes", dismissiveText: "No").Result;
                return (retval.HasValue && retval.Value == true);
            });
    }
}
  1. Rebuild and run the code on a device or simulator and try the menu button. You will only see the "MenuSelected" message appear. Even with breakpoints in every possible execution path of the command, the only breakpoint that will fire is the one in the MenuSelected event handler.

Configuration

I have only tested it on Android and iOS because those are my only targets for this project. Version: 1.6.5

Platform:

  • [x] :iphone: iOS
  • [x] :robot: Android
  • [x] :monkey: Xamarin.Forms
  • [ ] :checkered_flag: WPF
  • [ ] :earth_americas: UWP
  • [ ] :apple: MacOS
  • [ ] :tv: tvOS

lwg-jmarciniak avatar Jun 16 '20 00:06 lwg-jmarciniak

@lwg-jmarciniak Hi there

  • I have used this plugin in my project and (command) it's working fine. You can check working sample here: XF.Material Sample

Platform:

  • [x] 📱 iOS
  • [x] 🤖 Android

Debug point is working fine

  • CommandParameter is not required but if you want to pass it use CommandParameter="{Binding .}"

Debug point is working fine

divyesh008 avatar Sep 21 '20 17:09 divyesh008