summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/NavigationPage.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Core/NavigationPage.cs')
-rw-r--r--Xamarin.Forms.Core/NavigationPage.cs72
1 files changed, 22 insertions, 50 deletions
diff --git a/Xamarin.Forms.Core/NavigationPage.cs b/Xamarin.Forms.Core/NavigationPage.cs
index 7663d1bf..7b393b9c 100644
--- a/Xamarin.Forms.Core/NavigationPage.cs
+++ b/Xamarin.Forms.Core/NavigationPage.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
-using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;
@@ -28,10 +27,7 @@ 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));
@@ -65,23 +61,17 @@ namespace Xamarin.Forms
internal Task CurrentNavigationTask { get; set; }
- Page INavigationPageController.Peek(int depth)
+ Stack<Page> INavigationPageController.StackCopy
{
- if (depth < 0)
- {
- return null;
- }
-
- if (PageController.InternalChildren.Count <= depth)
+ get
{
- return null;
+ var result = new Stack<Page>(PageController.InternalChildren.Count);
+ foreach (Page page in PageController.InternalChildren)
+ result.Push(page);
+ return result;
}
-
- return (Page)PageController.InternalChildren[PageController.InternalChildren.Count - depth - 1];
}
- IEnumerable<Page> INavigationPageController.Pages => PageController.InternalChildren.Cast<Page>();
-
int INavigationPageController.StackDepth
{
get { return PageController.InternalChildren.Count; }
@@ -95,12 +85,6 @@ 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);
@@ -313,14 +297,8 @@ 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($"{nameof(before)} must be a child of the NavigationPage", nameof(before));
+ throw new ArgumentException("before must be a child of the NavigationPage", "before");
if (PageController.InternalChildren.Contains(page))
throw new ArgumentException("Cannot insert page which is already in the navigation stack");
@@ -331,9 +309,6 @@ 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();
@@ -344,13 +319,15 @@ namespace Xamarin.Forms
if (((INavigationPageController)this).StackDepth == 1)
return;
- Element[] childrenToRemove = PageController.InternalChildren.Skip(1).ToArray();
- foreach (Element child in childrenToRemove)
+ var root = (Page)PageController.InternalChildren.First();
+
+ var childrenToRemove = PageController.InternalChildren.ToArray().Where(c => c != root);
+ foreach (var child in childrenToRemove)
PageController.InternalChildren.Remove(child);
- CurrentPage = RootPage;
+ CurrentPage = root;
- var args = new NavigationRequestedEventArgs(RootPage, animated);
+ var args = new NavigationRequestedEventArgs(root, animated);
EventHandler<NavigationRequestedEventArgs> requestPopToRoot = PopToRootRequestedInternal;
if (requestPopToRoot != null)
@@ -361,7 +338,8 @@ namespace Xamarin.Forms
await args.Task;
}
- PoppedToRoot?.Invoke(this, new PoppedToRootEventArgs(RootPage, childrenToRemove.OfType<Page>().ToList()));
+ if (PoppedToRoot != null)
+ PoppedToRoot(this, new PoppedToRootEventArgs(root, childrenToRemove.OfType<Page>().ToList()));
}
async Task PushAsyncInner(Page page, bool animated)
@@ -382,29 +360,24 @@ namespace Xamarin.Forms
await args.Task;
}
- Pushed?.Invoke(this, args);
+ if (Pushed != null)
+ Pushed(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 == null)
- throw new ArgumentNullException($"{nameof(page)} cannot be null.");
-
- if (page == CurrentPage && CurrentPage == RootPage)
+ if (page == CurrentPage && ((INavigationPageController)this).StackDepth <= 1)
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 calling PopAsync instead.");
+ Log.Warning("NavigationPage", "RemovePage called for CurrentPage object. This can result in undesired behavior, consider called PopAsync instead.");
PopAsync();
return;
}
@@ -413,11 +386,10 @@ namespace Xamarin.Forms
throw new ArgumentException("Page to remove must be contained on this Navigation Page");
EventHandler<NavigationRequestedEventArgs> handler = RemovePageRequestedInternal;
- handler?.Invoke(this, new NavigationRequestedEventArgs(page, true));
+ if (handler != null)
+ handler(this, new NavigationRequestedEventArgs(page, true));
PageController.InternalChildren.Remove(page);
- if (RootPage == page)
- RootPage = (Page)PageController.InternalChildren.First();
}
void SafePop()