WPF丢失了事件处理程序

问题描述 投票:0回答:1

我在.NET 4.7中有一个简单的WPF应用程序。主窗口侦听“Key Up”事件。如果按下Escape键,主窗口将自行关闭。这很好(至少它开始工作正常)。

主窗口还包含一个打开另一个窗口的Button(称为X)。 X只是一个空窗口。它没有事件处理程序。当X打开时,主窗口中的Button被禁用。一旦X关闭,Button就会重新启用。

这就是问题:一旦X关闭,主窗口的Key Up事件处理程序似乎就消失了......等等。除非我首先将焦点放在其中一个按钮上,否则我无法再按Escape关闭主窗口。如果我只是单击主窗口并按Escape键,它将不会关闭窗口。在显示Window X之前,情况并非如此。

事实证明,如果我没有禁用并重新启用Button,则在关闭X后Key Up事件处理程序继续工作 - 我仍然可以通过按Esc键可靠地关闭主窗口。

显然,我是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();
        }
    }
}
wpf
1个回答
0
投票

关闭CanvasWindow时,主窗口无法获得焦点。禁用该按钮时,窗口上的任何其他内容都无法调焦。

如果您从Windows任务栏中选择应用程序(最小化和恢复)或切换程序并使用Ctrl + Tab返回它可以工作。

重新启用后尝试给出clickedButton焦点:

            newWindow.Closed += (x, y) =>
            {
                clickedButton.IsEnabled = true;
                clickedButton.Focus();
            };
© www.soinside.com 2019 - 2024. All rights reserved.