EventTrigger没有为UserControl的Loaded事件触发

blu*_*utt 10 wpf mvvm

我有一个带有以下事件触发器的UserControl:

<UserControl x:Class="TestApp.Views.MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         >

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding OnLoadedCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)

它是通过UserControl的构造函数设置的(请忽略ServiceLocator ..这只是一个快速原型):

public MyUserControl()
{
    InitializeComponent();

    DataContext = ServiceLocator.Current.GetInstance<DirectorySearchViewModel>();
}
Run Code Online (Sandbox Code Playgroud)

在我的视图模型中,我有以下内容:

    public ICommand OnLoadedCommand { get; private set; }

    public MyUserControl()
    {
        OnLoadedCommand = new DelegateCommand(OnLoaded);
    }

    public void OnLoaded()
    {
    }
Run Code Online (Sandbox Code Playgroud)

OnLoaded永远不会被调用.如果我将EventName更改为..MouseDown,那么它可以工作,但它只适用于Loaded

很确定这是一个愚蠢的错误(发誓我过去曾做过这么多次),但现在似乎无法解决这个问题

小智 9

public MyUserControl()
{
    DataContext = ServiceLocator.Current.GetInstance<DirectorySearchViewModel>();
    InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)

贵族.