我有一个包含以下事件触发器的用户控件:
<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>
这是通过UserControl的构造函数进行设置的(请忽略ServiceLocator..这只是一个快速原型):
public MyUserControl()
{
InitializeComponent();
DataContext = ServiceLocator.Current.GetInstance<DirectorySearchViewModel>();
}
在我的视图模型中,我有以下内容:
public ICommand OnLoadedCommand { get; private set; }
public MyUserControl()
{
OnLoadedCommand = new DelegateCommand(OnLoaded);
}
public void OnLoaded()
{
}
OnLoaded永远不会被调用。如果我将EventName更改为..MouseDown,则它可以工作,但它只是不能在Loaded中工作。
我很确定这是一个愚蠢的错误(我发誓过去已经做过无数次),但现在似乎无法弄清楚。