Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Added PoppedToRootEventArgs to track popped pages when calling PopToRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
johankson committed Jul 11, 2016
1 parent 925fc0a commit c5b4434
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
25 changes: 25 additions & 0 deletions Xamarin.Forms.Core.UnitTests/NavigationUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,31 @@ public async Task TestPopToRoot ()
Assert.AreEqual (root, nav.CurrentPage);
}

[Test]
public async Task TestPopToRootEventArgs ()
{
var nav = new NavigationPage ();

List<Page> poppedChildren = null;
nav.PoppedToRoot += (sender, args) => poppedChildren = (args as PoppedToRootEventArgs).PoppedPages.ToList();

var root = new ContentPage {Content = new View ()};
var child1 = new ContentPage {Content = new View ()};
var child2 = new ContentPage {Content = new View ()};

await nav.PushAsync (root);
await nav.PushAsync (child1);
await nav.PushAsync (child2);

await nav.PopToRootAsync ();

Assert.IsNotNull (poppedChildren);
Assert.AreEqual (2, poppedChildren.Count);
Assert.Contains (child1, poppedChildren);
Assert.Contains (child2, poppedChildren);
Assert.AreEqual (root, nav.CurrentPage);
}

[Test]
public async Task TestStackCopy ()
{
Expand Down
6 changes: 4 additions & 2 deletions Xamarin.Forms.Core/NavigationPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ async Task PopToRootAsyncInner(bool animated)

var root = (Page)PageController.InternalChildren.First();

PageController.InternalChildren.ToArray().Where(c => c != root).ForEach(c => PageController.InternalChildren.Remove(c));
var childrenToRemove = PageController.InternalChildren.ToArray().Where(c => c != root);
foreach (var child in childrenToRemove)
PageController.InternalChildren.Remove(child);

CurrentPage = root;

Expand All @@ -335,7 +337,7 @@ async Task PopToRootAsyncInner(bool animated)
}

if (PoppedToRoot != null)
PoppedToRoot(this, new NavigationEventArgs(root));
PoppedToRoot(this, new PoppedToRootEventArgs(root, childrenToRemove.OfType<Page>().ToList()));
}

async Task PushAsyncInner(Page page, bool animated)
Expand Down
18 changes: 18 additions & 0 deletions Xamarin.Forms.Core/PoppedToRootEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;

namespace Xamarin.Forms
{
public class PoppedToRootEventArgs : NavigationEventArgs
{
public PoppedToRootEventArgs(Page page, IEnumerable<Page> poppedPages) : base(page)
{
if (poppedPages == null)
throw new ArgumentNullException(nameof(poppedPages));

PoppedPages = poppedPages;
}

public IEnumerable<Page> PoppedPages { get; private set; }
}
}
1 change: 1 addition & 0 deletions Xamarin.Forms.Core/Xamarin.Forms.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@
<Compile Include="IApplicationController.cs" />
<Compile Include="IAppIndexingProvider.cs" />
<Compile Include="ListStringTypeConverter.cs" />
<Compile Include="PoppedToRootEventArgs.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion docs/Xamarin.Forms.Core/Xamarin.Forms/NavigationPage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,9 @@
</ReturnValue>
<Docs>
<summary>Event that is raised when the last nonroot element is popped from this <see cref="T:Xamarin.Forms.NavigationPage" /> element.</summary>
<remarks>To be added.</remarks>
<remarks>The <see cref="T:Xamarin.Forms.NavigationEventArgs" /> can be cast to <see cref="T:Xamarin.Forms.PoppedToRootEventArgs" /> for
access to additional properties. For example, the list of popped pages.
</remarks>
</Docs>
</Member>
<Member MemberName="PopToRootAsync">
Expand Down
60 changes: 60 additions & 0 deletions docs/Xamarin.Forms.Core/Xamarin.Forms/PoppedToRootEventArgs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<Type Name="PoppedToRootEventArgs" FullName="Xamarin.Forms.PoppedToRootEventArgs">
<TypeSignature Language="C#" Value="public class PoppedToRootEventArgs : Xamarin.Forms.NavigationEventArgs" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi beforefieldinit PoppedToRootEventArgs extends Xamarin.Forms.NavigationEventArgs" />
<AssemblyInfo>
<AssemblyName>Xamarin.Forms.Core</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Base>
<BaseTypeName>Xamarin.Forms.NavigationEventArgs</BaseTypeName>
</Base>
<Interfaces />
<Docs>
<summary>EventArgs for the NavigationPage's PoppedToRoot navigation event.</summary>
<remarks>
The <see cref="E:Xamarin.Forms.NavigationPage.PoppedToRoot" /> passes <see cref="T:Xamarin.Forms.NavigationEventArgs" /> as the
event argument. This class can be cast to <see cref="T:Xamarin.Forms.PoppedToRootEventArgs" /> to allow for access to the
PoppedPages collection that exposes the pages that was popped.
</remarks>
<altmember cref="E:Xamarin.Forms.NavigationPage.PoppedToRoot" />
</Docs>
<Members>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public PoppedToRootEventArgs (Xamarin.Forms.Page page);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class Xamarin.Forms.Page page) cil managed" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="page" Type="Xamarin.Forms.Page" />
</Parameters>
<Docs>
<param name="page">The page that is the new root page.</param>
<summary></summary>
<remarks></remarks>
</Docs>
</Member>
<Member MemberName="PoppedPages">
<MemberSignature Language="C#" Value="public System.Collections.Generic.IEnumerable&lt;Xamarin.Forms.Page&gt; PoppedPages { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance class System.Collections.Generic.IEnumerable`1&lt;class Xamarin.Forms.Page&gt; PoppedPages" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Collections.Generic.IEnumerable&lt;Xamarin.Forms.Page&gt;</ReturnType>
</ReturnValue>
<Docs>
<summary>Gets a collection of pages that was removed from the navigation stack.</summary>
<value></value>
<remarks>
<para>For <see cref="E:Xamarin.Forms.NavigationPage.PoppedToRoot" /> this represents the pages
that were popped. The order of the pages represents the order of the stack that was popped. The first page in the
collection is the page that was closest to the root page.
</para>
</remarks>
</Docs>
</Member>
</Members>
</Type>

0 comments on commit c5b4434

Please sign in to comment.