Joe*_*ite 10 keyboard wpf caliburn.micro
如何让Caliburn.Micro将关键手势映射到我的ViewModel上的动作方法?
例如,我想实现一个选项卡式界面,我希望我的ShellViewModel有一个NewTab方法,用户应该可以通过按键盘上的Ctrl + T来调用它.
我知道完整的Caliburn框架支持手势,但我怎么能用Caliburn.Micro做到这一点?是否有某种方法可以将动作绑定到RoutedCommand(因为RoutedCommands已经支持输入手势)?或者其他一些获得手势支持的方式?
Gre*_*vec 12
我修改了示例以启用对全局键绑定的支持.您只需将以下代码添加到视图中:
<i:Interaction.Triggers>
<common:InputBindingTrigger>
<common:InputBindingTrigger.InputBinding>
<KeyBinding Modifiers="Control" Key="D"/>
</common:InputBindingTrigger.InputBinding>
<cl:ActionMessage MethodName="DoTheMagic"/>
</common:InputBindingTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
每当按下Ctr + D时,方法DoTheMagic将被执行.这是修改后的InputBindingTrigger代码:
public class InputBindingTrigger : TriggerBase<FrameworkElement>, ICommand
{
public static readonly DependencyProperty InputBindingProperty =
DependencyProperty.Register("InputBinding", typeof (InputBinding)
, typeof (InputBindingTrigger)
, new UIPropertyMetadata(null));
public InputBinding InputBinding
{
get { return (InputBinding) GetValue(InputBindingProperty); }
set { SetValue(InputBindingProperty, value); }
}
public event EventHandler CanExecuteChanged = delegate { };
public bool CanExecute(object parameter)
{
// action is anyway blocked by Caliburn at the invoke level
return true;
}
public void Execute(object parameter)
{
InvokeActions(parameter);
}
protected override void OnAttached()
{
if (InputBinding != null)
{
InputBinding.Command = this;
AssociatedObject.Loaded += delegate {
var window = GetWindow(AssociatedObject);
window.InputBindings.Add(InputBinding);
};
}
base.OnAttached();
}
private Window GetWindow(FrameworkElement frameworkElement)
{
if (frameworkElement is Window)
return frameworkElement as Window;
var parent = frameworkElement.Parent as FrameworkElement;
Debug.Assert(parent != null);
return GetWindow(parent);
}
}
Run Code Online (Sandbox Code Playgroud)
Caliburn.Micro的Actions机制建立在System.Windows.Interactivity之上.因此,您可以创建基于TriggerBase的自定义触发器来执行任何操作,包括全局键盘手势.然后,只需将ActionMessage插入触发器和中提琴!
| 归档时间: |
|
| 查看次数: |
7648 次 |
| 最近记录: |