【问题标题】:WPF add shortcut to Controls.PageWPF 将快捷方式添加到 Controls.Page
【发布时间】:2019-07-14 07:32:37
【问题描述】:

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

但这两个函数永远不会被调用。

我有以下实现: Fitst NavigationWindow 被调用,它的源属性如下: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");
    }

我做错了什么?

这甚至可以在页面上绑定快捷方式吗?


编辑

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();
}

【问题讨论】:

  • 你为什么不直接实现一个 icommand 与你的键绑定一起工作?
  • 我没有找到 icommand/keybindings 实现的工作示例

标签: c# wpf keyboard-shortcuts


【解决方案1】:

试试这个:

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);
    }
}

【讨论】:

  • 我没有 RelayCommand 类,你能给我一个例子吗? (对不起,这是我的第一个 wpf 项目)
  • 为你添加了课程
  • 还是不行。如果我理解得很好:在我的 XAML 中,我将一个键绑定到一个 ICommand。在我的构造函数中,我创建了一个 RelayCommand 类的实例,其中参数为函数的名称。但是我如何在 ICommand 和 RelayCommand 的实例之间建立链接? (PS:我不得不替换 Page.InputBindings 中的 Window.InputBindings)
  • 我在答案中有一个错误,我更正了我忘记将名称更改为 doSomething 只是为了确保它不是与页面相关的问题 如果错误仍然存​​在,请在窗口上尝试我们太看不同的方向
  • 这段代码中的所有内容现在都有意义了。它仍然不能在 Page 上工作,但它可以在 MainWindow 上工作。所以麻烦来自佩奇。我不得不从 Page 调用它,因为我需要访问 Page 实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多