将卸载事件绑定到命令 - WPF

Tho*_*chl 5 c# wpf events binding command

我目前正在开发基于 MVVM 的 WPF 应用程序。

我这样定义一个用户控件:

        <UserControl x:Class="TestMockUp.Views.DriveStartupViews.VehicleIdentificationViewModel"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                 xmlns:ViewModels="clr-namespace:TestMockUp.ViewModels.DriveStartupViewModels"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800" d:DataContext="{d:DesignInstance ViewModels:VehicleIdentificationViewModel}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Unloaded">
                <i:InvokeCommandAction Command="{Binding PageUnloaded}"/>
            </i:EventTrigger>
            <i:EventTrigger EventName="Loaded">
                <i:InvokeCommandAction Command="{Binding PageLoaded}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
         ...
    </UserControl>
Run Code Online (Sandbox Code Playgroud)

我想做的是将命令挂钩到用户控件的卸载事件。我对窗口或应用程序是否关闭不感兴趣。

我根据需要将 UserControl 和 DataTemplate 加载到窗口中。如果控件被卸载,我需要注册。我可以将其绑定到 xaml.cs 文件中的事件处理程序,但我需要在 ViewModel 中调用命令。

已触发已加载事件的触发器,但未触发已卸载事件的触发器。我的猜测是,<i:Interaction.Triggers>在事件触发之前已经卸载,因此它永远无法注册。

我发现了一些有关 AttachedBehaviour 的内容,但无法使其适用于 Unloaded。

有谁知道一种方法,可以在不获取任何 Nuget 包或第三方软件的情况下实现这一点?

编辑:加载的命令被触发,卸载的命令触发视图内的 event_handler (如果我声明它),但不是触发命令<i:Interaction.Triggers>