MahApps Metro捕获关闭窗口事件

问题描述 投票:4回答:4

我使用的是MahApps的Metro窗口风格,我想捕捉用户点击窗口的关闭按钮时的事件。

我已经将ShutdownMode设置为OnExplicitShutdown,所以我需要调用 Application.Current.Shutdown(); 当该按钮被点击时

我如何才能做到这一点?

wpf mahapps.metro
4个回答
1
投票

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

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

1
投票

你需要创建一个DependencyProperty来处理关闭窗口的行为。

你需要创建一个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);
        }
    }
}

在您的ViewModel中

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
投票

因为我没有找到如何在程序上做到这一点(我的窗口没有Xaml文件,它只是一个基类),所以gottapps.net的解决方案对我来说并不适用。我找到了另一个变通的方法,用同样的按钮来关闭窗口,如下所示。

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);
        }
}

0
投票

你可以使用 "关闭 "或 "关闭 "事件。

当用户点击关闭按钮时,关闭处理程序会被触发。这也让你可以控制应用程序是否应该被关闭。

同样,在窗口关闭之前,关闭处理程序也会被触发。

<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>
© www.soinside.com 2019 - 2024. All rights reserved.