c# - 如何在Caliburn.Micro ViewModel中处理WPF MenuItem命令?

标签 c# wpf xaml mvvm caliburn.micro

我的WPF应用程序有一个带有以下代码的布局页面:

/* /Views/ShellView.xaml */
<DockPanel>
  <!-- Global Main menu, always visible -->
  <Menu IsMainMenu="true" DockPanel.Dock="Top">
    <MenuItem Header="_File">
      <MenuItem Command="Save"/>
      <MenuItem Header="_Save As..."/>
      <Separator/>
      <MenuItem Header="_Exit"/>
    </MenuItem>
  </Menu>

  <!-- The window's main content. It contains forms that the user might want to save -->
  <ContentControl x:Name="ActiveItem"/>
</DockPanel>
由于使用的是Caliburn.Micro,因此我还实现了相应的 View 模型:
/* /ViewModels/ShellViewModel.cs */
public class ShellViewModel : Conductor<object> {
  // Control logic to manage ActiveItem
}
我的目标是为Save命令实现一个处理程序,我认为只要用户单击相应的MenuItem或按CTRL + S就会触发该处理程序。
在标准WPF中,我将在CommandBinding中添加一个ShellView.xaml标记(如this tutorial中所示),以将该事件路由到我将在ShellView.xaml.cs中实现的处理程序。但是,为了尊重Caliburn.Micro的MVVM约定,我希望我的逻辑保留在 View 模型类之内。
我查看了Caliburn.Micro的文档,但发现与命令最接近的是Actions
我该如何实现?
谢谢你的时间。

最佳答案

解决方案并不短!
您需要创建一个依赖项(这里是GestureMenuItem)
在xaml文件中

 xmlns:common="clr-namespace:Common.Caliburn"
 :
 :
  <Menu IsMainMenu="true" DockPanel.Dock="Top">
     <common:GestureMenuItem x:Name="Save" Key="S" Modifiers="Ctrl" Header="_Save"/>

在ActionMessageCommand.cs文件中
using System;
using System.Windows.Input;
using Caliburn.Micro;

namespace Common.Caliburn
{
    public class ActionMessageCommand : ActionMessage, ICommand
    {
        static ActionMessageCommand()
        {
            EnforceGuardsDuringInvocation = true;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            
        }

        void ICommand.Execute(object parameter)
        {
        }

        public event EventHandler CanExecuteChanged;
    }
}

在GestureMenuItem.cs文件中
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace Common.Caliburn
{
    public class GestureMenuItem : MenuItem
    {
        public override void EndInit()
        {
            Interaction.GetTriggers(this).Add(ConstructTrigger());

            if(string.IsNullOrEmpty(InputGestureText))
                InputGestureText = BuildInputGestureText(Modifiers, Key);

            base.EndInit();
        }

        private static readonly IEnumerable<ModifierKeys> ModifierKeysValues = Enum.GetValues(typeof(ModifierKeys)).Cast<ModifierKeys>().Except(new [] { ModifierKeys.None });

        private static readonly IDictionary<ModifierKeys, string> Translation = new Dictionary<ModifierKeys, string>
        {
            { ModifierKeys.Control, "Ctrl" }
        };

        private static string BuildInputGestureText(ModifierKeys modifiers, Key key)
        {
            var result = new StringBuilder();

            foreach (var val in ModifierKeysValues)
                if ((modifiers & val) == val)
                    result.Append((Translation.ContainsKey(val) ? Translation[val] : val.ToString()) + " + ");

            result.Append(key);

            return result.ToString();
        }

        private TriggerBase<FrameworkElement> ConstructTrigger()
        {
            var trigger = new InputBindingTrigger();

            trigger.GlobalInputBindings.Add(new KeyBinding { Modifiers = Modifiers, Key = Key });

            var command = new ActionMessageCommand { MethodName = Name };
            Command = command;
            trigger.Actions.Add(command);

            return trigger;
        }

        public static readonly DependencyProperty ModifiersProperty =
            DependencyProperty.Register("Modifiers", typeof(ModifierKeys), typeof(GestureMenuItem), new PropertyMetadata(default(ModifierKeys)));

        public ModifierKeys Modifiers
        {
            get { return (ModifierKeys)GetValue(ModifiersProperty); }
            set { SetValue(ModifiersProperty, value); }
        }

        public static readonly DependencyProperty KeyProperty =
            DependencyProperty.Register("Key", typeof(Key), typeof(GestureMenuItem), new PropertyMetadata(default(Key)));

        public Key Key
        {
            get { return (Key)GetValue(KeyProperty); }
            set { SetValue(KeyProperty, value); }
        }
    }
}

在InputBindingTrigger.cs文件中
using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using Caliburn.Micro;

namespace Common.Caliburn
{
    public class InputBindingTrigger : TriggerBase<FrameworkElement>, ICommand
    {
        public InputBindingTrigger()
        {
            GlobalInputBindings = new BindableCollection<InputBinding>();
            LocalInputBindings = new BindableCollection<InputBinding>();
        }

        public static readonly DependencyProperty LocalInputBindingsProperty =
            DependencyProperty.Register("LocalInputBindings", typeof(BindableCollection<InputBinding>), typeof(InputBindingTrigger), new PropertyMetadata(default(BindableCollection<InputBinding>)));

        public BindableCollection<InputBinding> LocalInputBindings
        {
            get { return (BindableCollection<InputBinding>)GetValue(LocalInputBindingsProperty); }
            set { SetValue(LocalInputBindingsProperty, value); }
        }

        public BindableCollection<InputBinding> GlobalInputBindings
        {
            get { return (BindableCollection<InputBinding>)GetValue(GlobalInputBindingProperty); }
            set { SetValue(GlobalInputBindingProperty, value); }
        }

        public static readonly DependencyProperty GlobalInputBindingProperty =
            DependencyProperty.Register("GlobalInputBinding", typeof(BindableCollection<InputBinding>), typeof(InputBindingTrigger), new UIPropertyMetadata(null));

        protected override void OnAttached()
        {
            foreach (var binding in GlobalInputBindings.Union(LocalInputBindings))
                binding.Command = this;

            AssociatedObject.Loaded += delegate
            {
                var window = GetWindow(AssociatedObject);

                foreach (var binding in GlobalInputBindings)
                    window.InputBindings.Add(binding);

                foreach (var binding in LocalInputBindings)
                    AssociatedObject.InputBindings.Add(binding);
            };

            base.OnAttached();
        }

        private Window GetWindow(FrameworkElement frameworkElement)
        {
            if (frameworkElement is Window)
                return frameworkElement as Window;

            var parent = frameworkElement.Parent as FrameworkElement;

            return GetWindow(parent);
        }

        bool ICommand.CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            InvokeActions(parameter);
        }
    }
}
在ShellViewModel.cs中,使用Caliburn的约定名称
    public void Save()
    {
        //some code here
    }

关于c# - 如何在Caliburn.Micro ViewModel中处理WPF MenuItem命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62898832/

相关文章:

c# - UWP:替代 Grid.IsSharedSizeScope 和 SharedSizeGroup

c# - Windows 10 开发 : How to refresh ListView whenever there is a change in the items inside ListView?

c# - 重试 C# HttpClient 不成功的请求和超时

c# - 服务层应该返回什么类型的结果?

c# - 如何防止 resharper 8 使用 nunit 运行并行测试?

c# - Xamarin - 显示来自 base64 字符串的图像

WPF绑定(bind): How to avoid "property not found" runtime errors at design time

xaml - windows phone 8.1 ComboBox 当项目数量更多时,将多个项目显示为已选择

c# - 如何让 Label 显示带有不同颜色字母的 FormattedString?

c# - 接口(interface)、继承、隐式运算符和类型转换,为什么会这样?