For the first time I'm restricting the onAppearing()
methods in all child pages of tabbed page. I need to call the onAppearing()
when I change the tab. For that, I'm using OnCurrentPageChanged()
to call the onAppearing()
method.
When I change the tab, I'm calling the OnCurrentPageChanged()
and giving them access to run the onAppearing()
functionality. onAppearing()
is calling before calling the OnCurrentPageChanged()
.
TabbedPage code:
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();
}
}
}
Week Page code(child page):
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();
}
}
Here I need to call OnCurrentPageChanged()
method first instead of the OnApearing()
method. And OnCurrentPageChanged()
will give the bool
value to perform the code which is in OnApearing()
method.
In Android, onAppearing
is called before OnCurrentPageChanged
while in iOS OnCurrentPageChanged
is called before onAppearing
.
1.As jgoldberger
suggested, you can call method in each Page after 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.You can use messaging-center to notify specific page to perform some actions after 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();
}
And in each page:
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
}
}
BTW, you should use if...else if...else
instead of if...if...else
in your control statement.