C#键盘快捷键退出调用第二XAML窗口工作后, [英] C# Keyboard Shortcuts quit working after calling 2nd XAML window

查看:227
本文介绍了C#键盘快捷键退出调用第二XAML窗口工作后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用多个XAML窗口不同实例软件:例如,出口的一些信息,我创建XAML二次窗户,比母亲软件不同的格式。它们工作得很好。



我的问题是,如果我用我的软件,而不调用任何这些二级XAML窗口,快捷方式工作得很好。但是,一旦我把这种新的XAML窗口,快捷方式不工作了。我需要重新启动该程序使他们的复出还活着。



任何关于这种行为的线索?
此外,我不能没有像CTRL +字母创建快捷方式为例。我看到很多代码,这似乎是相当直接的,但他们就是不工作...



代码



 私人无效Window_KeyDown(对象发件人,发送KeyEventArgs E)
{
键键= e.Key;


如果((键== Key.Left)及和放大器; previousButton.IsEnabled)
button_PreviewMouseDown(previousButton,NULL);
,否则如果((键== Key.Right)及和放大器; nextButton.IsEnabled)
button_PreviewMouseDown(Next按钮,NULL);
//新建标签
,否则如果(键== Key.L)
//否则,如果(键== Key.LeftAlt&放大器;&安培; e.Key.ToString()== L)
NewLabel_Click(发件人,E);
//开始事件
,否则如果(键== Key.B)
BeginEvent_Click(发件人,E);
//结束事件
,否则如果(键== Key.E)
EndEvent_Click(发件人,E);
//删除标签
,否则如果(键== Key.K)
DeleteLabel_Click(发件人,E);
,否则如果(键== Key.R)
//删除事件
DeleteEvent_Click(发件人,E);
//编辑标签
,否则如果(键== Key.I)
EditLabel_Click(发件人,E);
//编辑事件
,否则如果(键== Key.F)
EditEvent_Click(发件人,E);
}



编辑1



我现在只要我打电话只是说事件创建OK快捷方式卷土重来再放一个C#弹出消息框,找到了!

  MessageBox.Show(事件创建); 



任何想法,为什么这可能发生?


解决方案

另一种方法是使用 RoutedCommands
,其中A的RoutedCommand分配 KeyGestures



例如

  ///<总结> 
///保存日志命令
///< /总结>
公共静态只读的RoutedCommand SaveLog =
新的RoutedCommand(SaveLog的typeof(命令),
新InputGestureCollection
{
新KeyGesture(Key.S,ModifierKeys。控制,保存日志内容保存到一个文件(CTL + S)。)
});



分配的RoutedCommand在XAML

 <按钮样式={StaticResource的LoggingWindowSaveLogButton}命令={X:静态地方:Commands.SaveLog}/> 



绑定命令到你的窗口

 <! - 命令绑定 - > 
< Window.CommandBindings>
<命令的CommandBinding ={X:静态地方:Commands.SaveLog}执行的=SaveLogCommandCanExecute =SaveLogCommandCanExecute/>
< /Window.CommandBindings>



然后实施 SaveLogCommandCanExecute SaveLogCommand



如果SaveLogCommandCanExecute回报它会自动禁用该的RoutedCommand势必任何GUI。



<记p>请具有键绑定的必须有焦点。


I have a software that uses multiple XAML windows for different instances: for example, to export some information, I've created secondary XAML windows with different format than the mother software. They work fine.

My problem is that, if I use my software without calling any of these secondary XAML windows, the shortcuts work pretty well. But as soon as I call this new XAML windows, the shortcuts don't work anymore. I need to restart the program so that they comeback alive.

Any clue on this behaviour? In addition, I was not able yet to create shortcuts like CTRL+Letter for example. I have seen plenty of codes, it seems pretty straight-forward but they just don't work...

Code

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    Key key = e.Key;


    if ((key == Key.Left) && previousButton.IsEnabled)
        button_PreviewMouseDown(previousButton, null);
    else if ((key == Key.Right) && nextButton.IsEnabled)
        button_PreviewMouseDown(nextButton, null);
    //New Label
    else if (key == Key.L)
        //else if (key == Key.LeftAlt && e.Key.ToString() == "L")
        NewLabel_Click(sender, e);
    // Begin Event
    else if (key == Key.B)
        BeginEvent_Click(sender, e);
    // End Event
    else if (key == Key.E)
        EndEvent_Click(sender, e);
    // Delete Label
    else if (key == Key.K)
        DeleteLabel_Click(sender, e);
    else if (key == Key.R)
        // Delete Event
        DeleteEvent_Click(sender, e);
    // Edit Label
    else if (key == Key.I)
        EditLabel_Click(sender, e);
    // Edit Event
    else if (key == Key.F)
        EditEvent_Click(sender, e);
}

EDIT 1

I found out now that as soon as I call a C# popup message box just saying "Event Created OK" the shortcuts comeback alive again!

MessageBox.Show("Event Created");

Any idea why this might be happening?

解决方案

Another way to do this is to use RoutedCommands. With a RoutedCommand you assign KeyGestures.

example

/// <summary>
/// Save Log command
/// </summary>
public static readonly RoutedCommand SaveLog =
    new RoutedCommand("SaveLog", typeof(Commands),
        new InputGestureCollection 
        {
            new KeyGesture(Key.S, ModifierKeys.Control,"Save log contents to a file. (Ctl+S)")
        });

Assign the RoutedCommand in your xaml

            <Button Style="{StaticResource LoggingWindowSaveLogButton}" Command ="{x:Static local:Commands.SaveLog}" />

Bind the command to your window

    <!-- Command Bindings-->
<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:Commands.SaveLog}" Executed="SaveLogCommand" CanExecute="SaveLogCommandCanExecute"/>
</Window.CommandBindings>

Then implement SaveLogCommandCanExecute and SaveLogCommand

if SaveLogCommandCanExecute returns false it will automatically disable any GUI that the RoutedCommand is bound to.

Keep in mind the window that has the keybinding MUST have focus.

这篇关于C#键盘快捷键退出调用第二XAML窗口工作后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆