Baseflow / Baseflow/XF-Material-Library
MaterialMenuButton - assigned Command is not being called on menu selection
- Dominant language
- C#
- Stars
- 633
- Forks
- 150
- PR merge metrics
- No merged PRs in 30d
Description
## 🐛 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`
```
```
2. 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");
}
}
}
```
3. 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);
});
}
}
```
4. 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
Contributor guide
Assessment
This issue has not been assessed yet.