MahApps Metro捕获关闭窗口事件

4

我正在使用MahApps Metro窗口样式,我想在用户单击窗口的关闭按钮时捕获事件。

我将我的ShutdownMode设置为OnExplicitShutdown,因此当单击该按钮时,我需要调用Application.Current.Shutdown();

我该如何做到这一点?

4个回答

1

由于gotapps.net的解决方案对我没有用,因为我不知道如何以编程方式实现它(我的窗口没有Xaml文件,它只是一个基类)。我找到了另一种解决方法,使用同一个按钮关闭窗口,如下所示:

internal class BaseWindow : MetroWindow
{
        public BaseWindow()
        {
            this.Loaded += BaseWindow_Loaded;
        }

        void BaseWindow_Loaded(object sender, EventArgs e)
        {
            Button close = this.FindChild<Button>("PART_Close");
            close.Click += close_Click;
        }

        void close_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown(0);
        }
}

1
你需要创建一个DependencyProperty来处理关闭窗口的行为:
依赖属性。
namespace MyApp.DependencyProperties
{
    public class WindowProperties
    {
        public static readonly DependencyProperty WindowClosingProperty =
           DependencyProperty.RegisterAttached("WindowClosing", typeof(RelayCommand), typeof(WindowProperties), new UIPropertyMetadata(null, WindowClosing));

        public static object GetWindowClosing(DependencyObject depObj)
        {
            return (RelayCommand)depObj.GetValue(WindowClosingProperty);
        }

        public static void SetWindowClosing(DependencyObject depObj, RelayCommand value)
        {
            depObj.SetValue(WindowClosingProperty, value);
        }

        private static void WindowClosing(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            var element = (Window)depObj;

            if (element != null)
                element.Closing += OnWindowClosing;

        }

        private static void OnWindowClosing(object sender, CancelEventArgs e)
        {
            RelayCommand command = (RelayCommand)GetWindowClosing((DependencyObject)sender);
            command.Execute((Window)sender);
        }
    }
}

在你的视图模型中。
public RelayCommand WindowClosedCommand { get; set; }

private void WindowClose()
{
    Application.Current.Shutdown();
}

在 ViewModel 的构造函数中。
this.WindowCloseCommand = new RelayCommand(WindowClose);

在你的XAML中。
<mah:MetroWindow x:Class="MyApp.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:dp="clr-namespace:MyApp.DependencyProperties"
        dp:WindowProperties.WindowClosing="{Binding WindowClosedCommand}" />

1

我相信我也在尝试使用WPF和MahApps.Metro做与你相同的事情(绑定关闭窗口按钮)。我还没有找到一种明确绑定到该命令的方法,但是我通过将ShowCloseButton属性设置为false(隐藏它),然后创建自己的关闭窗口命令按钮并在我的视图模型中处理逻辑来实现了这个目标。我花了一些时间查找,但我发现你可以很容易地使用MahApps.Metro在命令栏中添加自己的窗口命令控件,只需在你的XAML中添加类似的标记:

<Controls:MetroWindow.WindowCommands>
    <Controls:WindowCommands>
        <Button Content="X" Command="{Binding CancelCommand}" />
    </Controls:WindowCommands>
</Controls:MetroWindow.WindowCommands>

0

您可以使用“Closing”或“Closed”事件

当用户单击关闭按钮时,将触发Closing处理程序。这还使您对应用程序是否应关闭具有控制权。

同样,当窗口关闭之前,将触发Closed处理程序。

<Controls:MetroWindow x:Class="MyClass.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="My Class"
Closing="MainWindow_OnClosing">
</Controls:MetroWindow>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接