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

时间:2020-07-14 15:39:45

标签: 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以来,我还实现了相应的视图模型:

/* /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约定,我希望我的逻辑保留在视图模型类之内。

我研究了Caliburn.Micro的文档,但发现与命令最接近的是Actions

我该如何实现?

感谢您的时间。

2 个答案:

答案 0 :(得分:0)

解决方案不短!

您需要创建一个依赖项(这里是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
    }

答案 1 :(得分:0)

我找到了一个更短的解决方案。在继承ViewAware的类上,当视图准备就绪时,将使用视图对象调用受保护的虚拟方法onViewReady

所以在我的视图模型中,我可以像这样覆盖它:

protected override void OnViewReady(object view) {
    base.OnViewReady(view);

    ShellView shellView = (ShellView)view;
    shellView.CommandBindings.Add(new CommandBinding(ApplicationCommands.SaveAs, SaveAsCommandHandler));
}

使用视图对象,可以通过指定特定的命令和处理程序来添加新的CommandBinding实例。

可以在视图模型类中定义处理程序:

private void SaveAsCommandHandler(object sender, ExecutedRoutedEventArgs e) {
    this.CurrentForm.SaveAs();
}