Как обрабатывать команды WPF MenuItem в Caliburn.Micro ViewModel? - PullRequest
1 голос
/ 14 июля 2020

В моем приложении 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 (как показано в этом руководстве ) для перенаправления события в обработчик, который я бы реализовал в ShellView.xaml.cs. Однако ради соблюдения соглашений MVVM Caliburn.Micro я хочу, чтобы мой logi c оставался в пределах класса модели представления.

Я просмотрел документацию Caliburn.Micro, но самое близкое, что у меня есть. найдено, что команды были Действия .

Как я могу это реализовать?

Спасибо за ваше время.

Ответы [ 2 ]

0 голосов
/ 18 июля 2020

Я нашел более короткое решение. При наследовании класса ViewAware защищенный виртуальный метод onViewReady вызывается с объектом представления, когда представление готово.

Итак, в моей модели представления я могу просто переопределить его следующим образом:

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

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

С помощью объекта представления я могу добавить новый CommandBinding экземпляр, указав специфицированную c команду и обработчик.

Обработчик может быть определен в классе модели представления:

private void SaveAsCommandHandler(object sender, ExecutedRoutedEventArgs e) {
    this.CurrentForm.SaveAs();
}
0 голосов
/ 16 июля 2020

Решение не короткое !!

Вам нужно создать зависимость (здесь 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
    }
...