When you use MVVM Light in WPF or UWP you can use triggers like whne you navigate to a page use the trigger event Load.
Like this:
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{Binding LoadedCommand}" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
And this are the namespaces:
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
But how can you do this in Xamarin.Forms
? Is it the same? I can't find a example.
UPDATE
My xaml page:
Reference to EventToCommandBehavior:
xmlns:behaviors="clr-namespace:XamarinTest.Behaviors"
<ContentPage.Behaviors>
<behaviors:EventToCommandBehavior EventName="OnAppearing"
Command="{Binding LoadedCommand}" />
</ContentPage.Behaviors>
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
And this is my ViewModel:
public RelayCommand LoadedCommand { get; }
public UserMainViewModel()
{
LoadedCommand = new RelayCommand(LoadData);
}
private async void LoadData()
{
//...
}
And the binding between the xaml Page and the ViewModel is like this in Page1.xaml.cs:
public Page1()
{
InitializeComponent();
BindingContext = App.Locator.UserMainViewModel;
}
Error:
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00017] in <657aa8fea4454dc898a9e5f379c58734>:0
at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0007a] in <657aa8fea4454dc898a9e5f379c58734>:0
at System.Reflection.MonoCMethod.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <657aa8fea4454dc898a9e5f379c58734>:0
at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <657aa8fea4454dc898a9e5f379c58734>:0
at MyProject.Services.NavigationService.NavigateTo (System.String pageKey, System.Object parameter) [0x000e8] in C:\Projects\MyProject\MyProject\MyProject\Services\NavigationService.cs:89
at MyProject.Services.NavigationService.NavigateTo (System.String pageKey) [0x00001] in C:\Projects\MyProject\MyProject\MyProject\Services\NavigationService.cs:42
at MyProject.ViewModels.LoginViewModel.Login () [0x00002] in C:\Projects\MyProject\MyProject\MyProject\ViewModels\LoginViewModel.cs:25
You have to use EventToCommandBehavior as described here https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/behaviors/reusable/event-to-command-behavior/
And implement the following xaml in your page: OnAppearing exists only on pages. (No Loaded event)
<ContentPage.Behaviors>
<local:EventToCommandBehavior EventName="Appearing" Command="{Binding LoadedCommand}" />
<ContentPage.Behaviors>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.