OnApearing() 方法未在 xamarin 表单的 Mater 详细信息页面中第二次触发

OnApearing() method is not firing second time in Mater details page of xamarin forms

我正在使用自定义主详细信息页面。在那个页面上,我实现了 onAppearing() 方法。它是第一次触发(当我 运行 时 program.Master 详细信息页面是我应用程序的根页面)。 当我单击汉堡包图标打开导航抽屉时,OnAppearing() 未触发。

MasterDetailsPage CS 代码:

public partial class MainMasterDetailPage : MasterDetailPage
    {
        public MainMasterDetailPage()
        {
            InitializeComponent();
            
            NavigationPage.SetHasBackButton(this, false);
            NavigationPage.SetHasNavigationBar(this, false);
           
            ListOfSettings.IsVisible = false;

            this.IsPresented = false;

            Detail = new NavigationPage(new DashboardTabbedPage());

        }
        protected override async void OnAppearing()
        {
            base.OnAppearing();
            ListOfSettings.IsVisible = false;
        }
}

除了消息中心(在传递 bool 值时可能会很棘手)之外,处理此问题的最简单方法是在菜单页面中订阅主详细信息页面的 IsPresentedChanged 事件处理程序。示例伪代码:

namespace TestApp.Views
{
    public partial class MenuPage : ContentPage
    {
        MainPage RootPage { get => Application.Current.MainPage as MainPage; }
        List<HomeMenuItem> menuItems;
        public MenuPage()
        {
            InitializeComponent();

            BindingContext = this;

            menuItems = new List<HomeMenuItem>
            {
                new HomeMenuItem {Id = MenuItemType.Browse, Title="Browse" },
                new HomeMenuItem {Id = MenuItemType.About, Title="About" }
            };




            ListViewMenu.ItemsSource = menuItems;

            ListViewMenu.SelectedItem = menuItems[0];
            ListViewMenu.ItemSelected += async (sender, e) =>
            {
                if (e.SelectedItem == null)
                    return;

                var id = (int)((HomeMenuItem)e.SelectedItem).Id;
                RootPage.IsPresentedChanged += MenuVisibleChanged;
                await RootPage.NavigateFromMenu(id);

            };


        }

        private void MenuVisibleChanged(object sender, EventArgs e)
        {
            //handle your menu visibilty here with calling IsPresented
            var menuIsVisible = RootPage.IsPresented;
        }
    }
}

我用了IsPresentedChanged属性。请检查以下代码。

public MainMasterDetailPage()
        {
            InitializeComponent();

            NavigationPage.SetHasBackButton(this, false);
            NavigationPage.SetHasNavigationBar(this, false);
            ListOfSettings.IsVisible = false;
            this.IsPresented = false;
            Detail = new NavigationPage(new DashboardTabbedPage());

            this.IsPresentedChanged += hideSettingMenu;

        }
        public void hideSettingMenu(object sender, EventArgs args)
        {
            ListOfSettings.IsVisible = false;
        }