diff --git a/Xamarin.Forms.Core.UnitTests/PageTests.cs b/Xamarin.Forms.Core.UnitTests/PageTests.cs index b69428387..d63a78967 100644 --- a/Xamarin.Forms.Core.UnitTests/PageTests.cs +++ b/Xamarin.Forms.Core.UnitTests/PageTests.cs @@ -400,6 +400,39 @@ namespace Xamarin.Forms.Core.UnitTests Assert.True (completed); } + class PageTestApp : Application { } + + [Test] + public void SendApplicationPageAppearing() + { + var app = new PageTestApp(); + var page = new ContentPage(); + + Page actual = null; + app.MainPage = page; + app.PageAppearing += (sender, args) => actual = args; + + ((IPageController)page).SendAppearing(); + + Assert.AreSame(page, actual); + } + + [Test] + public void SendApplicationPageDisappearing() + { + var app = new PageTestApp(); + var page = new ContentPage(); + + Page actual = null; + app.MainPage = page; + app.PageDisappearing += (sender, args) => actual = args; + + ((IPageController)page).SendAppearing(); + ((IPageController)page).SendDisappearing(); + + Assert.AreSame(page, actual); + } + [Test] public void SendAppearing () { @@ -494,5 +527,5 @@ namespace Xamarin.Forms.Core.UnitTests Assert.True (sentNav); Assert.True (sent); } - } + } } diff --git a/Xamarin.Forms.Core/Application.cs b/Xamarin.Forms.Core/Application.cs index 95462f169..0fa9f3df5 100644 --- a/Xamarin.Forms.Core/Application.cs +++ b/Xamarin.Forms.Core/Application.cs @@ -169,6 +169,10 @@ namespace Xamarin.Forms public event EventHandler ModalPushing; + public event EventHandler PageAppearing; + + public event EventHandler PageDisappearing; + public async Task SavePropertiesAsync() { if (Device.IsInvokeRequired) @@ -279,6 +283,12 @@ namespace Xamarin.Forms return properties; } + internal void OnPageAppearing(Page page) + => PageAppearing?.Invoke(this, page); + + internal void OnPageDisappearing(Page page) + => PageDisappearing?.Invoke(this, page); + void OnModalPopped(Page modalPage) { EventHandler handler = ModalPopped; diff --git a/Xamarin.Forms.Core/Page.cs b/Xamarin.Forms.Core/Page.cs index e839f6d14..c485b45db 100644 --- a/Xamarin.Forms.Core/Page.cs +++ b/Xamarin.Forms.Core/Page.cs @@ -317,6 +317,8 @@ namespace Xamarin.Forms var pageContainer = this as IPageContainer; pageContainer?.CurrentPage?.SendAppearing(); + + FindApplication(this)?.OnPageAppearing(this); } [EditorBrowsable(EditorBrowsableState.Never)] @@ -337,6 +339,16 @@ namespace Xamarin.Forms EventHandler handler = Disappearing; if (handler != null) handler(this, EventArgs.Empty); + + FindApplication(this)?.OnPageDisappearing(this); + } + + Application FindApplication(Element element) + { + if (element == null) + return null; + + return (element.Parent is Application app) ? app : FindApplication(element.Parent); } void InternalChildrenOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) @@ -416,4 +428,4 @@ namespace Xamarin.Forms return _platformConfigurationRegistry.Value.On(); } } -} \ No newline at end of file +} diff --git a/docs/Xamarin.Forms.Core/Xamarin.Forms/Application.xml b/docs/Xamarin.Forms.Core/Xamarin.Forms/Application.xml index f763d45ed..130f98e5f 100644 --- a/docs/Xamarin.Forms.Core/Xamarin.Forms/Application.xml +++ b/docs/Xamarin.Forms.Core/Xamarin.Forms/Application.xml @@ -363,6 +363,36 @@ To be added. + + + + Event + + 2.0.0.0 + + + System.EventHandler<Xamarin.Forms.Page> + + + To be added. + To be added. + + + + + + Event + + 2.0.0.0 + + + System.EventHandler<Xamarin.Forms.Page> + + + To be added. + To be added. + + @@ -455,7 +485,7 @@ System.Diagnostics.DebuggerStepThrough - System.Runtime.CompilerServices.AsyncStateMachine(typeof(Xamarin.Forms.Application/<SavePropertiesAsync>d__54)) + System.Runtime.CompilerServices.AsyncStateMachine(typeof(Xamarin.Forms.Application/<SavePropertiesAsync>d__60))