代码之家  ›  专栏  ›  技术社区  ›  Brum

来自代码隐藏的WPF调用窗口关闭事件

  •  0
  • Brum  · 技术社区  · 6 年前

    最近我一直在尝试WPF。我正在建立一个小程序,但后来我偶然发现了一个小问题。我试图从后面的代码中调用Window\u Closing方法,它告诉我需要给它cerntain参数,但是我在试图调用它的方法中没有这些参数。

    这是我的密码:

                private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
                {
                    // My Window Closing code
                }
    
                private void Application_Exit(object sender, RoutedEventArgs e)
                {
                    // Here is where I am trying to call it, giving a empty parameter with it.
                    // But it doesn't seem to work.        
                    Window_Closing(null, EventArgs.Empty)
                }
    

    我想要什么

    谁知道解决办法?

    3 回复  |  直到 6 年前
        1
  •  2
  •   Julian Peil    6 年前

    在你的 Designer ,单击 Button 打开它的 property window . 在这里您可以选择“ Events “闪电”并选择 Click Window_Closing 方法。有一个下拉列表,您可以在其中选择它。如果没有,请在那里输入您的方法名,然后按“回车”让VS生成代码隐藏方法。

        2
  •  1
  •   Praboda    6 年前

    <Window x:Class="WpfApp1.MainWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Closing="Window_Closing">
    <Grid>
        <Button x:Name="Close" Content="Close" Click="Close_Click" Width="100" Height="30"/>
    </Grid>
    

        public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
    
        }
    
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
    
        3
  •  0
  •   Andreas    6 年前

    把窗户关上。然后事件将被自动调用,这就是它的初衷。

    只要在窗口中放置一个断点,然后关闭,就会看到,它是自动执行的。如果没有,也许应该将eventhander添加到所有windows窗口关闭事件中。