我已经使用 FreshMVVM 实现了选项卡式导航。当我的应用程序启动时,我注意到所有选项卡都调用了“ViewIsAppearing”方法。但是,如果我切换到一个选项卡,则不会调用其 ViewModel 中的“ViewIsAppearing”方法。如果转到其他选项卡并切换回同一选项卡,则它可以工作。即“ViewIsAppearing”未在初始选项卡更改单击中调用。我如何让它在第一次尝试中调用。我遇到了与此类似的 github 问题。只需添加以供参考 https://github.com/xamarin/Xamarin.Forms/issues/3855
问问题
304 次
1 回答
0
正如@FreakyAli 所建议的,我通过创建自定义事件“OnAppearing”和“OnDisappearing”来解决问题。到目前为止它工作正常。下面提供的代码。请发表您的意见。
自定义选项卡式导航
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)
{
}
}
于 2020-05-22T14:33:26.257 回答