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

标签 xamarin xamarin.forms display tabbedpage

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

分页码:

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.作为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.您可以使用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在您的控制语句中。

关于xamarin - OnAppearing() 方法在调用 OnCurrentPageChanged() 方法之前执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59409853/

相关文章:

firebase - 错误 : package com. google.firebase.messaging 不存在

c# - 选择器的完成按钮的 Event_Handler

android - Android map 的奇怪行为

ios - Xamarin Essentials MediaPicker 不适用于 iOS

python - Pandas Dataframe 显示缺失的网格线(Jupyter Notebook)

jquery - 使用 jquery 根据页面高度显示 ul

ios - 在 iPhone 5 上尝试调试应用程序时出现 Xamarin iOS 错误

xaml - 将特定样式应用于 Xamarin.Forms 元素的所有子元素

java - 在java中执行linux命令并将输出显示到html表

ios - Xamarin-IOS BTLE WroteCharacteristicValue 未触发