C# Keyboard Shortcuts quit working after calling 2nd XAML window

倾然丶 夕夏残阳落幕 提交于 2019-12-10 14:25:50

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/36423870/c-sharp-keyboard-shortcuts-quit-working-after-calling-2nd-xaml-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!