Preferrably written in PRISM (Prism.Core 6.2, Prism.Windows 6.02), but I also would like to know how to bind Command to Page Loading/ Loaded event in UWP with normal MVVM without Prism.
In WPF, it is achievable with:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
In the ViewModel
public ICommand LoadedCommand { get; private set; }
public TheViewModelConstructor()
{
LoadedCommand = new DelegateCommand(Load);
}
private async void Load()
{
// Do stuff
}
But in UWP, System.Windows.Interactivity does not exist. Simply binding with
Loaded="{Binding LoadedCommand}"
or
Loading="{Binding LoadingCommand}"
will get compile error "Object reference not set an instance of an object".
The reason I wanted to do this is because there is an async method that needs to be done during or after Page is loaded (it cannot be inside ViewModel constructor). I could do it with code behind easily, but this is not MVVM.
How can I bind this Command properly?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…