因此,在相同的 View 和 View 模型中,我的其他按钮都以相同的方式装配,工作正常。
然而,这个恰好在上下文菜单中的菜单却没有。即使我将命令绑定(bind)更改为我知道不存在的东西,也没有任何绑定(bind)表达式错误,我觉得这非常有趣
XAML
<ListView SelectionMode="Single" ItemsSource="{Binding Path=DisplayImages}" >
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="ContextFlyout">
<Setter.Value>
<MenuFlyout>
<MenuFlyoutItem Text="Save Image" Icon="Save" Command="{Binding Path=SaveImageCommand}"/>
</MenuFlyout>
</Setter.Value>
</Setter>
</Style>
</ListView.Resources>
C#
public ICommand SaveImageCommand { get; set; }
SaveImageCommand = new CommandHandler(SaveImageExecuted, true);
private async void SaveImageExecuted()
{
}
我的命令处理程序
public class CommandHandler : ICommand
{
private Action _action;
private bool _canExecute;
public CommandHandler(Action action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
}
最佳答案
所以我很确定这是框架中的错误。
为了解决这个问题,我决定只使用点击处理程序来调用 View 模型中执行的命令。 super 简陋,但它确实有效。
XAML
<MenuFlyoutItem Text="Save Image" Icon="Save" Click="OnSaveContextMenuClicked"/>
代码背后
private void OnSaveContextMenuClicked(object sender, RoutedEventArgs e)
{
viewModel.SaveImageCommand.Execute(sender);
}
关于c# - 命令绑定(bind)在 Flyout 或 FlyoutMenu 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50554559/