XAML:
<ContentPage.Behaviors>
<toolkit:EventToCommandBehavior
EventName="OnAppearing"
Command="{Binding AppearingExeCommand}" />
</ContentPage.Behaviors>
ViewModel:
[RelayCommand]
public void AppearingExe()
{
try
{
WeakReferenceMessenger.Default.Reset();
if (!WeakReferenceMessenger.Default.IsRegistered<MyMessage>(this))
{
WeakReferenceMessenger.Default.Register<MyMessage>(this, OnMessageReceived);
}
}
catch(Exception er)
{
Shell.Current.DisplayAlert("Error!", er.Message, "OK");
}
}
Running an error:Exception has been thrown by the target of an invocation
What is the reason? How should I handle it
There is no OnAppearing
event on ContentPage, only a method with that name. You need to use the Appearing
event of ContentPage:
<ContentPage.Behaviors>
<toolkit:EventToCommandBehavior
EventName="Appearing"
Command="{Binding AppearingExeCommand}" />
</ContentPage.Behaviors>