Xamarin FreshMVVM - 选项卡式导航 ViewIsAppearing 方法未在初始选项卡单击时触发

Xamarin FreshMVVM - Tabbed Navigation ViewIsAppearing method not getting fired on initial tab click

我已经使用 FreshMVVM 实现了选项卡式导航。当我的应用程序启动时,我可以注意到 'ViewIsAppearing' 方法正在为所有选项卡调用。但是,如果我切换到一个选项卡,则不会调用其 ViewModel 中的 'ViewIsAppearing' 方法。如果转到其他选项卡并切换回同一选项卡,则可以正常工作。即 'ViewIsAppearing' 未在初始选项卡更改点击中被调用。我如何让它在第一次尝试时调用。 我遇到过与此类似的 github 问题。仅供参考 https://github.com/xamarin/Xamarin.Forms/issues/3855

正如@FreakyAli 所建议的,我通过创建自定义事件 'OnAppearing' 和 'OnDisappearing' 来解决这个问题。到目前为止它工作正常。下面提供的代码。请post发表意见。

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; }
}   

基本页面模型

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());
    }
}

示例页面模型

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)
    {

    }
}