diff options
author | adrianknight89 <adrianknight89@outlook.com> | 2017-02-02 09:35:23 -0600 |
---|---|---|
committer | Kangho Hur <kangho.hur@samsung.com> | 2017-03-24 13:18:57 +0900 |
commit | 3b608a3a5e9b5739014a60995c31dbf610b61de4 (patch) | |
tree | 7fa83323820fe480e7eadeffabc92d5a2e0b087b /Xamarin.Forms.Core | |
parent | 22ebe38f70c038ef89ce8f1453ece5d222e685a8 (diff) | |
download | xamarin-forms-3b608a3a5e9b5739014a60995c31dbf610b61de4.tar.gz xamarin-forms-3b608a3a5e9b5739014a60995c31dbf610b61de4.tar.bz2 xamarin-forms-3b608a3a5e9b5739014a60995c31dbf610b61de4.zip |
[Core] Added RootPage to NavigationPage (#464)
* d
* removed whitespace
* Using ArgumentNullException
* changes
Diffstat (limited to 'Xamarin.Forms.Core')
-rw-r--r-- | Xamarin.Forms.Core/NavigationPage.cs | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/Xamarin.Forms.Core/NavigationPage.cs b/Xamarin.Forms.Core/NavigationPage.cs index fdafec06..7663d1bf 100644 --- a/Xamarin.Forms.Core/NavigationPage.cs +++ b/Xamarin.Forms.Core/NavigationPage.cs @@ -28,7 +28,10 @@ namespace Xamarin.Forms static readonly BindablePropertyKey CurrentPagePropertyKey = BindableProperty.CreateReadOnly("CurrentPage", typeof(Page), typeof(NavigationPage), null); public static readonly BindableProperty CurrentPageProperty = CurrentPagePropertyKey.BindableProperty; - + + static readonly BindablePropertyKey RootPagePropertyKey = BindableProperty.CreateReadOnly(nameof(RootPage), typeof(Page), typeof(NavigationPage), null); + public static readonly BindableProperty RootPageProperty = RootPagePropertyKey.BindableProperty; + public NavigationPage() { _platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<NavigationPage>>(() => new PlatformConfigurationRegistry<NavigationPage>(this)); @@ -92,6 +95,12 @@ namespace Xamarin.Forms private set { SetValue(CurrentPagePropertyKey, value); } } + public Page RootPage + { + get { return (Page)GetValue(RootPageProperty); } + private set { SetValue(RootPagePropertyKey, value); } + } + public static string GetBackButtonTitle(BindableObject page) { return (string)page.GetValue(BackButtonTitleProperty); @@ -304,8 +313,14 @@ namespace Xamarin.Forms void InsertPageBefore(Page page, Page before) { + if (page == null) + throw new ArgumentNullException($"{nameof(page)} cannot be null."); + + if (before == null) + throw new ArgumentNullException($"{nameof(before)} cannot be null."); + if (!PageController.InternalChildren.Contains(before)) - throw new ArgumentException("before must be a child of the NavigationPage", "before"); + throw new ArgumentException($"{nameof(before)} must be a child of the NavigationPage", nameof(before)); if (PageController.InternalChildren.Contains(page)) throw new ArgumentException("Cannot insert page which is already in the navigation stack"); @@ -316,6 +331,9 @@ namespace Xamarin.Forms int index = PageController.InternalChildren.IndexOf(before); PageController.InternalChildren.Insert(index, page); + if (index == 0) + RootPage = page; + // Shouldn't be required? if (Width > 0 && Height > 0) ForceLayout(); @@ -326,15 +344,13 @@ namespace Xamarin.Forms if (((INavigationPageController)this).StackDepth == 1) return; - var root = (Page)PageController.InternalChildren.First(); - - var childrenToRemove = PageController.InternalChildren.ToArray().Where(c => c != root); - foreach (var child in childrenToRemove) + Element[] childrenToRemove = PageController.InternalChildren.Skip(1).ToArray(); + foreach (Element child in childrenToRemove) PageController.InternalChildren.Remove(child); - CurrentPage = root; + CurrentPage = RootPage; - var args = new NavigationRequestedEventArgs(root, animated); + var args = new NavigationRequestedEventArgs(RootPage, animated); EventHandler<NavigationRequestedEventArgs> requestPopToRoot = PopToRootRequestedInternal; if (requestPopToRoot != null) @@ -345,8 +361,7 @@ namespace Xamarin.Forms await args.Task; } - if (PoppedToRoot != null) - PoppedToRoot(this, new PoppedToRootEventArgs(root, childrenToRemove.OfType<Page>().ToList())); + PoppedToRoot?.Invoke(this, new PoppedToRootEventArgs(RootPage, childrenToRemove.OfType<Page>().ToList())); } async Task PushAsyncInner(Page page, bool animated) @@ -367,24 +382,29 @@ namespace Xamarin.Forms await args.Task; } - if (Pushed != null) - Pushed(this, args); + Pushed?.Invoke(this, args); } void PushPage(Page page) { PageController.InternalChildren.Add(page); + if (PageController.InternalChildren.Count == 1) + RootPage = page; + CurrentPage = page; } void RemovePage(Page page) { - if (page == CurrentPage && ((INavigationPageController)this).StackDepth <= 1) + if (page == null) + throw new ArgumentNullException($"{nameof(page)} cannot be null."); + + if (page == CurrentPage && CurrentPage == RootPage) throw new InvalidOperationException("Cannot remove root page when it is also the currently displayed page."); if (page == CurrentPage) { - Log.Warning("NavigationPage", "RemovePage called for CurrentPage object. This can result in undesired behavior, consider called PopAsync instead."); + Log.Warning("NavigationPage", "RemovePage called for CurrentPage object. This can result in undesired behavior, consider calling PopAsync instead."); PopAsync(); return; } @@ -393,10 +413,11 @@ namespace Xamarin.Forms throw new ArgumentException("Page to remove must be contained on this Navigation Page"); EventHandler<NavigationRequestedEventArgs> handler = RemovePageRequestedInternal; - if (handler != null) - handler(this, new NavigationRequestedEventArgs(page, true)); + handler?.Invoke(this, new NavigationRequestedEventArgs(page, true)); PageController.InternalChildren.Remove(page); + if (RootPage == page) + RootPage = (Page)PageController.InternalChildren.First(); } void SafePop() |