WPF将快捷方式添加到Controls.Page

时间:2019-02-20 14:58:30

标签: c# wpf keyboard-shortcuts

我想将两个快捷方式绑定到我代码的后面。

但是从不调用这两个函数。

我有以下实现: Fitst NavigationWindow被调用,它具有如下的source属性:Source="MainPage.xaml"

MainPage.xaml代码:

<Page x:Class="XXX.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:local="clr-namespace:XXX"
  mc:Ignorable="d" 
  d:DesignHeight="450" d:DesignWidth="800"
  Title="MainPage"  DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
        <RoutedUICommand x:Key="Ctr1" Text="Another Text" />
        <RoutedUICommand x:Key="Ctr2" Text="Another Text" />
    </ResourceDictionary>
</Page.Resources>

<Page.InputBindings>
    <KeyBinding Key="F10" Modifiers="Ctrl" Command="{StaticResource Ctr1}" />
    <KeyBinding Key="F12" Modifiers="Ctrl" Command="{StaticResource Ctr2}" />
</Page.InputBindings>

<Page.CommandBindings>
    <CommandBinding Command="{StaticResource Ctr1}" Executed="CtrShortcut1" />
    <CommandBinding Command="{StaticResource Ctr2}" Executed="CtrShortcut2" />
</Page.CommandBindings>
<Grid/>
</Page>

MainPage.xaml.cs代码:

    public MainPage()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public void CtrShortcut1(Object sender, ExecutedRoutedEventArgs e)
    {
        FONCTIONS.ShowToast("success", "test");
    }

    public void CtrShortcut2(Object sender, ExecutedRoutedEventArgs e)
    {
        FONCTIONS.ShowToast("success", "test2");
    }

我做错了什么?

这是否有可能在Page上绑定快捷方式?


编辑

xaml代码:

<Page.InputBindings>
    <KeyBinding Key="F12" Command="{Binding DoSomething}"/>
</Page.InputBindings>

xaml.cs代码:

 public ICommand DoSomething { get; set; }        

    private void doSomething(object obj)
    {
        FONCTIONS.ShowToast("success", "Shortcut work !");
    }

    public MainPage()
    {
        InitializeComponent();
        this.DataContext = this;
        DoSomething = new RelayCommand(doSomething);
    }

解决方案 我找不到从Controls.Page调用快捷方式的方法 但它可用于NavigationWindow和Window。 要获取页面的上下文,我在静态变量中引用了当前页面,然后调用StaticVariable.Foo()

页面代码:

  public static dynamic CurrentPage;
    public MainPage()
        {
            InitializeComponent();
            this.DataContext = this;
            FONCTIONS.CurrentPage = this;
        }

在我的MainWindow代码中:

private void CurrentPageSetup1(object obj)
{
   REF_TO_STATIC_CLASS.CurrentPage.OpenSetup();
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

XAML:

    <Window.InputBindings>
       <KeyBinding Key="F5" Command="{Binding DoSomething}"/> 
    </Window.InputBindings>

代码/数据上下文:

    public ICommand DoSomething{ get; set; }

    DoSomething= new RelayCommand(doSomething);

    private void doSomething(object obj)
    {
        //Make it happen
    }

我想您已经有一个RelayCommand类了吗?还是您也需要它?

编辑 在这里:

class RelayCommand : ICommand
{
    private readonly Action<object> execute;

    private readonly Predicate<object> canExecute;
    private ICommand setCommand;

    public RelayCommand(Action<object> execute) : this(execute, null)
    {
    }

    public RelayCommand(ICommand setCommand)
    {
        this.setCommand = setCommand;
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            // ReSharper disable once LocalizableElement
            throw new ArgumentNullException(nameof(execute), "Execute method missing");
        }

        this.execute = execute;
        this.canExecute = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }

        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }

    public bool CanExecute(object parameter)
    {
        return this.canExecute?.Invoke(parameter) ?? true;
    }

    public void Execute(object parameter)
    {
        this.execute(parameter);
    }
}