0
votes

I have implemented tabbed navigation using FreshMVVM. When my app launches, I could notice that the 'ViewIsAppearing' method is getting invoked for all the tabs. However, if I switch to one tab, the 'ViewIsAppearing' method in its ViewModel is not getting called. If go to some other tab and switch back to this same tab, then it works. i.e. 'ViewIsAppearing' is not getting invoked in the initial tab change click. How do I make it invoke in the first attempt itself. I have come across a github issue similar to this. Just adding for reference https://github.com/xamarin/Xamarin.Forms/issues/3855

1
This is actually an issue with XF and since internally Fresh uses the same events to trigger its events until this issue is not solved the one in fresh will not work! - FreakyAli
Hi , not understanding this : "However, if I switch to one tab, the 'ViewIsAppearing' method in its ViewModel is not getting called" . Do you mean this happens after app launched ,then select another tab that the 'ViewIsAppearing' method not called ? - Junior Jiang
Yes, after the app launches and if I move to another tab, the 'ViewIsAppearing' for that tab page is not getting called. - Ashok
@FreakyAli - Any workarounds available? - Ashok
Well, you could use the CurrentPageChanged to know which page you are on and trigger a custom event! - FreakyAli

1 Answers

0
votes

As suggested by @FreakyAli, I did a workaround by creating custom events 'OnAppearing' and 'OnDisappearing'. Its working fine so far. Code provided below. Please post your comments.

CustomTabbedNavigation

public class FreshBottomTabbedNavigationContainer:FreshTabbedNavigationContainer
{
    private BasePageModel lastPageModel;

    public FreshBottomTabbedNavigationContainer()
    {
        On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
        this.CurrentPageChanged += FreshBottomTabbedNavigationContainer_CurrentPageChanged;
    }

    private void FreshBottomTabbedNavigationContainer_CurrentPageChanged(object sender, EventArgs e)
    {
        NavigationPage currentPage = (NavigationPage) ((FreshBottomTabbedNavigationContainer)sender).CurrentPage;
        BasePageModel model = (BasePageModel)currentPage.RootPage.GetModel();

        if (lastPageModel == null)
        {
            model.TriggerPageChangedEvent(new PageChangeEventArgs { CurrentPageModel = model });
        }
        else
        {
            model.TriggerPageChangedEvent(new PageChangeEventArgs { CurrentPageModel = model, LastPageModel = lastPageModel});
        }

        lastPageModel = model;
    }
}

PageChangeEventArgs

public class PageChangeEventArgs
{
    public BasePageModel CurrentPageModel { get; set; }
    public BasePageModel LastPageModel { get; set; }
}   

Base Page Model

public class BasePageModel : FreshBasePageModel
{
    public event EventHandler OnAppearing;
    public event EventHandler OnDisappearing;

    public BasePageModel()
    {

    }


    public void TriggerPageChangedEvent(PageChangeEventArgs e)
    {
        e.CurrentPageModel.OnAppearing?.Invoke(e.CurrentPageModel.CurrentPage, new EventArgs());
        e.LastPageModel?.OnDisappearing?.Invoke(e.LastPageModel.CurrentPage, new EventArgs());
    }
}

Sample Page Model

public class SamplePageModel : BasePageModel, INotifyPropertyChanged
{
    public SamplePageModel()
    {
        OnAppearing += SamplePageModel_OnAppearing;
        OnDisappearing += SamplePageModel_OnDisappearing;
    }

    private void SamplePageModel_OnDisappearing(object sender, EventArgs e)
    {

    }

    private void SamplePageModel_OnAppearing(object sender, EventArgs e)
    {

    }
}