OnAppearing() 方法在调用 OnCurrentPageChanged() 方法之前执行

OnAppearing() method is performing before calling the OnCurrentPageChanged() method

我第一次在标签页的所有子页面中限制 onAppearing() 方法。更改选项卡时,我需要调用 onAppearing()。为此,我使用 OnCurrentPageChanged() 来调用 onAppearing() 方法。 当我更改选项卡时,我会调用 OnCurrentPageChanged() 并授予他们访问 运行 和 onAppearing() 功能的权限。 onAppearing() 在调用 OnCurrentPageChanged() 之前调用。

TabbedPage代码:

public partial class VendorScheduleTabbedPage : Xamarin.Forms.TabbedPage
    {
       public int isCount;
        public VendorScheduleTabbedPage ()
        {
            InitializeComponent ();
            Xamarin.Forms.Application.Current.Properties["dayOnAppear"] = false;
            Xamarin.Forms.Application.Current.Properties["weekOnAppear"] = false;
            Xamarin.Forms.Application.Current.Properties["monthOnAppear"] = false;
            On<Android>().SetBarItemColor(value: Color.FromHex("#6699FF"));
            On<Android>().SetBarSelectedItemColor(value: Color.Orange);
        }
        override protected void OnCurrentPageChanged()
        {
            isCount = 1;
            if (this.CurrentPage.Title == "Week")
            {
                Xamarin.Forms.Application.Current.Properties["weekOnAppear"] = true;

            }
            if (this.CurrentPage.Title == "Month")
            {
                Xamarin.Forms.Application.Current.Properties["monthOnAppear"] = true;
            }
            else
            {
                Xamarin.Forms.Application.Current.Properties["dayOnAppear"] = true;
            }
            base.OnCurrentPageChanged();
        }
    }
}

周页代码(子页):

public WeekSchedulePage()
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);
            timeSlot = new List<VendorScheduleTimeSlot>();
            scheduleSlots = new List<VendorScheduleTimeSlot>();
            lstVendorsData = new List<ScheduledCustomersVM>();
            SortedList = new List<ScheduledCustomersVM>();
            scheduledCustomersList = new List<ScheduledCustomersVM>();
            rescheduledCustomersList = new List<RescheduledCustomersVM>();
            ConfirmBtn.IsVisible = true;
            ConfirmBtn.IsEnabled = false;

            vendorDayAndHoursDataVM = new VendorDayAndHoursDataVM();
            lstDaysAndHours = new List<VendorDayAndHoursDataVM>();
            lstQuestionsData = new List<VendorQuestionsDataVM>();
            overlay.IsVisible = false;
            Alert.IsVisible = false;

            presentWeekDay.Text = DateTime.Now.ToString("dddd, dd MMMM yyyy");

            currentDayName = DateTime.Now.DayOfWeek.ToString();
            currentDate = DateTime.Parse(presentWeekDay.Text);

        }

        protected override void OnAppearing()
        {
            var isAppear = Convert.ToBoolean(Application.Current.Properties["weekOnAppear"].ToString());
            if (isAppear == true)
            {
                ConfirmBtn.IsVisible = true;
                ConfirmBtn.IsEnabled = false;

                overlay.IsVisible = false;
                Alert.IsVisible = false;

                Application.Current.Properties["dayOnAppear"] = true;
                Application.Current.Properties["monthOnAppear"] = true;
                ConfirmBtn.IsEnabled = false;
                scheduledCustomersList.Clear();
                rescheduledCustomersList.Clear();

                presentWeekDay.Text = DateTime.Now.ToString("dddd, dd MMMM yyyy");

                currentDayName = DateTime.Now.DayOfWeek.ToString();

                weekwiseTimeslotClick();

                base.OnAppearing();
            }

        }

这里我需要先调用OnCurrentPageChanged()方法而不是OnApearing()方法。 OnCurrentPageChanged() 将给出 bool 值来执行 OnApearing() 方法中的代码。

在Android中,onAppearingOnCurrentPageChanged之前调用,而在iOS中,OnCurrentPageChangedonAppearing之前调用。

1.As jgoldberger建议,可以在CurrentPageChanged:

之后的每个Page中调用方法
    override protected void OnCurrentPageChanged()
    {
        if (this.CurrentPage.Title == "Week")
        {
            Xamarin.Forms.Application.Current.Properties["weekOnAppear"] = true;

            NavigationPage naviPage = this.Children[0] as NavigationPage;
            WeekPage page = naviPage.RootPage as WeekPage;
            page.test();

        }else if (this.CurrentPage.Title == "Month")
        {
            Xamarin.Forms.Application.Current.Properties["monthOnAppear"] = true;

            NavigationPage naviPage = this.Children[1] as NavigationPage;
            MonthPage page = naviPage.RootPage as MonthPage;
            page.test();
        }
        else
        {
            Xamarin.Forms.Application.Current.Properties["dayOnAppear"] = true;

            NavigationPage naviPage = this.Children[2] as NavigationPage;
            DayPage page = naviPage.RootPage as DayPage;
            page.test();
        }

        base.OnCurrentPageChanged();
    }
}

2.You可以使用messaging-center通知特定页面在CurrentPageChanged之后执行一些操作:

    override protected void OnCurrentPageChanged()
    {
        string testStr;

        if (this.CurrentPage.Title == "Week")
        {
            testStr = "Week";
        }else if (this.CurrentPage.Title == "Month")
        {
            testStr = "Month";
        }
        else
        {
            testStr = "Day";
        }

        MessagingCenter.Send<object, string>(new object(), "CurrentPageChanged", testStr);

        base.OnCurrentPageChanged();
    }

并且在每一页中:

public partial class MonthPage : ContentPage
{
    public MonthPage()
    {
        InitializeComponent();

        MessagingCenter.Subscribe<object, string>(new object(), "CurrentPageChanged", async (sender, arg) =>
        {
            if (arg == "Month")
            {
                Console.WriteLine(arg);
                //do something
            }
        });
    }

    public void test() {

        Console.WriteLine("test");
        //do something
    }
}

顺便说一句,您应该在控制语句中使用 if...else if...else 而不是 if...if...else