OnAppearing()方法正在执行,然后调用OnCurrentPageChanged()方法

问题描述 投票:2回答:1

这是我第一次限制选项卡式页面的所有子页面中的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()方法中的代码。

xamarin xamarin.forms display tabbedpage
1个回答
0
投票

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

1。按照jgoldberger的建议,您可以在CurrentPageChanged之后的每个页面中调用方法:

    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

© www.soinside.com 2019 - 2024. All rights reserved.