开发者

mvvm-light toolkit not firing Window Loaded event

I have a Window and using MVVM light toolkit to bind to the Loaded event:

    开发者_StackOverflow中文版<Window 
      ...
      xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
      xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
      ...
      >

      <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
          <cmd:EventToCommand Command="{Binding Loaded, Mode=OneWay}" />
        </i:EventTrigger>
      </i:Interaction.Triggers>
    </Window>

and in my view-model, I have the following:

private ICommand _loaded;
public ICommand Loaded
{
  get
  {
    if (_loaded == null)
    {
      _loaded = CreateAsyncRelayCommand(p => OnLoaded());
    }
    return _loaded;
  }
}

public void OnLoaded()
{
  throw new NotImplementedException();
}

but on the window showing, the exception isn't thrown although the ICommand variable is being initialized when I put a break point in the Loaded property. I've tried changing to using the Initialized event, but still the same thing. This works fine in another Window I have in the same app, so I'm almost sure I'm doing everything correctly - could there be something I'm missing? When I bind with code-behind, it works fine, but I want to adhere to the mvvm pattern. Thanks in advance.


Try assigning the command inside your constructor, i.e.

public ViewModel() {
    _loaded = CreateAsyncRelayCommand(p => OnLoaded());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜