WPF失去了活动处理程序



我在.NET 4.7中有一个简单的WPF应用程序。主窗口听"键UP"事件的聆听。如果按下逃生键,主窗口将自身关闭。这可以正常工作(至少它开始工作正常)。

主窗口还包含一个打开另一个窗口的按钮(调用x)。X只是一个空窗口。它没有活动处理程序。打开X时,主窗口中的按钮被禁用。X关闭后,该按钮将重新启用。

这是问题:X关闭后,主窗口的键UP事件处理程序似乎已经消失了。除非我首先将焦点放在其中一个按钮上,否则我不能再按下逃脱以关闭主窗口。如果我只单击主窗口并按Escaver,它将不会关闭窗口。在显示窗口x之前,情况并非如此。

事实证明,如果我做禁用并重新启用按钮,则关闭X关闭后的密钥UP事件处理程序将继续工作 - 我仍然可以通过按下来可靠地关闭主窗口逃生键。

显然,我是WPF的新手。我想知道Windows事件是否有些奇怪。

这是主窗口的XAML,后面是其背后的代码。我还包括Window X的XAML和代码(BTW,Window X是WrapPanelWindow)。

<Window x:Class="Wpf_01.MainWindow" x:ClassModifier="internal"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Wpf_01"
    mc:Ignorable="d"
    Title="Main Window" Height="300" Width="300" Background="#FF6289A4" KeyUp="Key_Up">
<StackPanel Margin="20,10,20,10">
    <Button Content="Canvas Example"/>
    <Button Content="WrapPanel Example" Click="Button_Click"/>
    <Button Content="DockPanel Example"/>
    <Button Content="Grid Example"/>
    <Button Content="StackPanel Example"/>
</StackPanel>

这是背后的主窗口代码:

namespace Wpf_01 {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    internal partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e) {
            Button clickedButton = sender as Button;
            Window newWindow = null;
            switch(clickedButton.Content) {
                case "Canvas Example":
                    newWindow = new CanvasWindow();
                    break;
                case "WrapPanel Example":
                    newWindow = new WrapPanelWindow();
                    break;
            }
            if(newWindow != null) {
                clickedButton.IsEnabled = false;
                newWindow.Show();
                newWindow.Closed += (x, y) => clickedButton.IsEnabled = true;
            }
        } // Button_Clicked()

        private void Key_Up(object sender, KeyEventArgs e) {
            if(e.Key == Key.Escape)
                Close();
        } // KeyHandler()
    }
}

这是wrappanelwindow xaml和代码背后:

<Window x:Class="Wpf_01.WrapPanelWindow" x:ClassModifier="internal"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wpf_01"
        mc:Ignorable="d"
        Title="Fun with WrapPanel Window" Height="200" Width="260">
    <Grid>
    </Grid>
</Window>

最后,wrappanelwindow的代码背后。

namespace Wpf_01 {
    /// <summary>
    /// Interaction logic for WrapPanelWindow.xaml
    /// </summary>
    internal partial class WrapPanelWindow : Window {
        public WrapPanelWindow() {
            InitializeComponent();
        }
    }
}

当您关闭CanvasWindow时,主窗口不会使焦点恢复。当您禁用按钮时,窗口上的其他任何内容都不可聚焦。

如果您从Windows任务栏(最小化和还原)或Switch程序中选择该应用程序,然后使用Ctrl Tab返回,则可以工作。

重新启用后,请尝试将单击焦点焦点提供:

            newWindow.Closed += (x, y) =>
            {
                clickedButton.IsEnabled = true;
                clickedButton.Focus();
            };

最新更新