WPF MVVM处理窗口关闭'X'

时间:2016-09-18 22:02:44

标签: c# wpf mvvm

我在很多帖子中都读过这个,但仍然不明白如何在'X'处理窗口关闭。例如。我需要在结束前提问。

我尝试使用“互动”:

<Window x:Class="Sem5_CP.Views.AirlineManagerView"
        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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:local="clr-namespace:test.Views"
        mc:Ignorable="d"
        Title="AirlineManagerView" Height="300" Width="300">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Closing">
            <i:InvokeCommandAction Command="{Binding ExitCommand}"  />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid Style="{StaticResource MainGridStyle}">
        <StackPanel Style="{StaticResource MainStackPanelStyle}">
            <Button Content="Exit" Command="{Binding ExitCommand}" />
        </StackPanel>
    </Grid>
</Window>

但是{i}无法识别Command属性:InvokeCommandAction。仅限CommandNameCommandParameter

这是ViewModel代码:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using Sem5_CP.Views;

namespace Sem5_CP.ViewModels
{
    public class AirlineManagerViewModel : ViewModel
    {
        public ICommand OpenFleetManagerCommand { get; private set; }

        private readonly List<Window> childWindows = new List<Window>(); 

        public AirlineManagerViewModel(CloseWindowDelegate close)
            : base(close)
        {
            ExitCommand = new DelegateCommand(Exit);
            OpenFleetManagerCommand = new DelegateCommand(OpenFleetManager);
        }

        private void OpenFleetManager()
        {
            FleetManagerView fleetManager = new FleetManagerView();
            childWindows.Add(fleetManager);
            fleetManager.Show();
        }

        protected override void Exit()
        {
            var result = MessageBox.Show("Exit?", "Exit", MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                childWindows.ForEach(w => w.Close());
                childWindows.Clear();
                CloseWindow();
            }
        }
    }
}

0 个答案:

没有答案