从 WinForm 应用程序调用 WPF KeyBindings 时,我需要一些帮助。我已经创建了我认为是演示问题的基本部分。如果有帮助,我可以提供一个示例应用程序。
WinForm 应用程序启动一个表单,该表单有一个调用 WPF 的按钮
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim view As New WpfPart.MainWindow
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(view)
view.ShowDialog()
End Sub
使用 WPF 视图创建它的视图模型并设置键控:
<Window x:Class="WpfPart.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfPart.ViewModels"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
<Window.InputBindings>
<KeyBinding Key="Escape" Command="{Binding OpenCommand}" Modifiers="Control" />
</Window.InputBindings>
<Grid>
</Grid>
ViewModel 使用 DelagateCommand 希望将所有内容链接起来
using System;
using System.Windows;
using System.Windows.Input;
using WpfPart.Commands;
namespace WpfPart.ViewModels
{
class MainWindowViewModel
{
private readonly ICommand openCommand;
public MainWindowViewModel()
{
openCommand = new DelegateCommand(Open, CanOpenCommand);
}
public ICommand OpenCommand { get { return openCommand; } }
private bool CanOpenCommand(object state)
{
return true;
}
private void Open(object state)
{
MessageBox.Show("OpenCommand executed.");
}
}
}
谁能看到哪里出了问题,按键什么也没做?!?