C# mvvm 如何处理 window 关闭事件
C# mvvm how to handle window close events
我正在开发带有注释的 wpf mvvm 应用程序。面对我无法处理 viewModel 中的 window 关闭事件的事实。我发现了类似的问题,但答案使用了 Mvvm Light,我想避免这种情况。我可以这样处理:
FindNoteWindow.xaml
<Window x:Class="NotesARK6.View.FindNoteWindow"
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"
DataContext="{Binding Path=FindNoteWindowViewModel, Source={StaticResource ViewModelLocator}}"
mc:Ignorable="d"
Closing="Window_Closing"
Title="FindNoteWindow" Height="250" Width="400">
FindNoteWindow.xaml.cs
public partial class FindNoteWindow : Window
{
public FindNoteWindow()
{
InitializeComponent();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// code
}
}
但这不是我需要的。我想像这样处理 viewModel 中的关闭事件:
FindNoteWindow.xaml
<Window x:Class="NotesARK6.View.FindNoteWindow"
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"
DataContext="{Binding Path=FindNoteWindowViewModel, Source={StaticResource ViewModelLocator}}"
mc:Ignorable="d"
Closing="{Binding Window_Closing}"
Title="FindNoteWindow" Height="250" Width="400">
FindNoteWindowViewModel.cs
public void Window_Closing(object sender, CancelEventArgs e)
{
//code
}
但是如果我这样做,我会收到错误:InvalidCastException:无法将对象类型 'System.Reflection.RuntimeEventInfo' 转换为类型 'System.Reflection.MethodInfo'。
预先感谢您的回复。
您可以像这样将命令绑定到事件:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing" >
<i:InvokeCommandAction Command="{Binding WindowClosingCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
记得添加对System.Windows.Interactivity的引用:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
从示例和更多详细信息中查看 https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/
编辑:或调用 CallMethodAction
而不是 InvokeCommandAction
,同样的例子在 link
我正在开发带有注释的 wpf mvvm 应用程序。面对我无法处理 viewModel 中的 window 关闭事件的事实。我发现了类似的问题,但答案使用了 Mvvm Light,我想避免这种情况。我可以这样处理:
FindNoteWindow.xaml
<Window x:Class="NotesARK6.View.FindNoteWindow"
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"
DataContext="{Binding Path=FindNoteWindowViewModel, Source={StaticResource ViewModelLocator}}"
mc:Ignorable="d"
Closing="Window_Closing"
Title="FindNoteWindow" Height="250" Width="400">
FindNoteWindow.xaml.cs
public partial class FindNoteWindow : Window
{
public FindNoteWindow()
{
InitializeComponent();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// code
}
}
但这不是我需要的。我想像这样处理 viewModel 中的关闭事件:
FindNoteWindow.xaml
<Window x:Class="NotesARK6.View.FindNoteWindow"
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"
DataContext="{Binding Path=FindNoteWindowViewModel, Source={StaticResource ViewModelLocator}}"
mc:Ignorable="d"
Closing="{Binding Window_Closing}"
Title="FindNoteWindow" Height="250" Width="400">
FindNoteWindowViewModel.cs
public void Window_Closing(object sender, CancelEventArgs e)
{
//code
}
但是如果我这样做,我会收到错误:InvalidCastException:无法将对象类型 'System.Reflection.RuntimeEventInfo' 转换为类型 'System.Reflection.MethodInfo'。 预先感谢您的回复。
您可以像这样将命令绑定到事件:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing" >
<i:InvokeCommandAction Command="{Binding WindowClosingCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
记得添加对System.Windows.Interactivity的引用:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
从示例和更多详细信息中查看 https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/
编辑:或调用 CallMethodAction
而不是 InvokeCommandAction
,同样的例子在 link