MVVM call OnAppearing()

DeviC3DeviC3 ✭✭Member ✭✭

Is there way to call OnAppearing() event in my ViewModel ?
I tried a lot but it looks like ther's no way.

Tagged:

Posts

  • JoeMankeJoeManke ✭✭✭✭✭ USMember ✭✭✭✭✭

    Why are you trying to do that?

  • DeviC3DeviC3 ✭✭ Member ✭✭
    In my viewmodel class. It should somehow inherit from Content Page
  • JoeMankeJoeManke ✭✭✭✭✭ USMember ✭✭✭✭✭

    But why? What do you intend to do that you need to call a lifecycle method instead of letting it be called by the framework?

  • DeviC3DeviC3 ✭✭ Member ✭✭
    Listview in page should be always updated so I think it is needed to call OnAppearing
  • JoeMankeJoeManke ✭✭✭✭✭ USMember ✭✭✭✭✭

    Sounds like you're trying to do work in your code-behind instead of your ViewModel. Instead, when OnAppearing is called, your page should call a ViewModel method to update your data and bindings.

  • DeviC3DeviC3 ✭✭ Member ✭✭

    Ok, now I think I understand, it's my second time in mvvm, tommorow I will try this and post code. Thank You for help for now.

  • AlessandroCaliaroAlessandroCaliaro ✭✭✭✭✭ ITMember ✭✭✭✭✭
    You can convert your event in a Command using behavior.forms package
  • NMackayNMackay mod GBInsider, University mod

    Take inspiration from Prismlib which has great hooks into navigation events

    https://medium.com/@hminaya/prism-101-navigation-8433eb30f578

    As @AlessandroCaliaro says, you could hook into the page's behaviors and execute a command

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
                 xmlns:behaviors="clr-namespace:Behaviors;assembly=Behaviors" 
                 x:Class="Behaviors.Sample.InvokeCommandDemoPage" 
                 Title="InvokeCommand Demo" 
                 Icon="xaml.png">
        <ContentPage.BindingContext>
            <local:InvokeCommandDemoPageViewModel />
        </ContentPage.BindingContext>
        ...
        <ContentPage.Behaviors>
            <behaviors:EventHandlerBehavior EventName="Appearing">
                <behaviors:InvokeCommandAction Command="{Binding PageAppearingCommand}" />
            </behaviors:EventHandlerBehavior>
            <behaviors:EventHandlerBehavior EventName="Disappearing">
                <behaviors:InvokeCommandAction Command="{Binding PageDisappearingCommand}" />
            </behaviors:EventHandlerBehavior>
        </ContentPage.Behaviors>
        ...
    </ContentPage>
    

    https://www.davidbritch.com/2016/04/xamarinforms-behaviors-support-for-page.html

Sign In or Register to comment.