[Shell] Apply Query string parameters even if they aren't present and correctly apply...
authorShane Neuville <shane94@hotmail.com>
Wed, 3 Jul 2019 23:57:46 +0000 (17:57 -0600)
committerSamantha Houts <samhouts@users.noreply.github.com>
Wed, 3 Jul 2019 23:57:46 +0000 (16:57 -0700)
* apply query string parameters if they aren't there
- apply query string parameters correctly to nested pages

* Update Xamarin.Forms.Core/Shell/Shell.cs

Co-Authored-By: Stephane Delcroix <stephane@delcroix.org>
fixes #6543

Xamarin.Forms.Core.UnitTests/ShellTestBase.cs
Xamarin.Forms.Core.UnitTests/ShellTests.cs
Xamarin.Forms.Core/Shell/Shell.cs
Xamarin.Forms.Core/Shell/ShellItem.cs
Xamarin.Forms.Core/Shell/ShellSection.cs

index 8b47fb3..d2ef648 100644 (file)
@@ -42,7 +42,11 @@ namespace Xamarin.Forms.Core.UnitTests
                [QueryProperty("SomeQueryParameter", "SomeQueryParameter")]
                public class ShellTestPage : ContentPage
                {
-                       public string SomeQueryParameter { get; set; }
+                       public string SomeQueryParameter
+                       {
+                               get;
+                               set;
+                       }
                }
 
                protected ShellItem CreateShellItem(TemplatedPage page = null, bool asImplicit = false, string shellContentRoute = null, string shellSectionRoute = null, string shellItemRoute = null)
index c4506fa..09d029d 100644 (file)
@@ -221,6 +221,64 @@ namespace Xamarin.Forms.Core.UnitTests
 
 
                [Test]
+               public async Task NavigationWithQueryStringThenWithoutQueryString()
+               {
+                       var shell = new Shell();
+
+                       var one = new ShellItem { Route = "one" };
+                       var two = new ShellItem { Route = "two" };
+
+                       var tabone = MakeSimpleShellSection("tabone", "content");
+                       var tabfour = MakeSimpleShellSection("tabfour", "content", null);
+
+                       one.Items.Add(tabone);
+                       two.Items.Add(tabfour);
+
+                       shell.Items.Add(one);
+                       shell.Items.Add(two);
+
+                       ShellTestPage pagetoTest = new ShellTestPage();
+                       await shell.GoToAsync(new ShellNavigationState($"//two/tabfour/content?{nameof(ShellTestPage.SomeQueryParameter)}=1234"));
+                       two.CurrentItem.CurrentItem.ContentTemplate = new DataTemplate(() =>
+                       {
+                               pagetoTest = new ShellTestPage();
+                               pagetoTest.BindingContext = pagetoTest;
+                               return pagetoTest;
+                       });
+
+
+                       await shell.GoToAsync(new ShellNavigationState($"//one/tabone/content"));
+                       await shell.GoToAsync(new ShellNavigationState($"//two/tabfour/content"));
+
+                       var page = (two.CurrentItem.CurrentItem as IShellContentController).GetOrCreateContent();
+                       Assert.AreEqual(null, (page as ShellTestPage).SomeQueryParameter);
+               }
+
+
+               [Test]
+               public async Task NavigationBetweenShellContentsPassesQueryString()
+               {
+                       var shell = new Shell();
+
+                       var item = CreateShellItem(shellSectionRoute: "section2");
+                       var content = CreateShellContent(shellContentRoute: "content");
+                       item.Items[0].Items.Add(content);
+
+                       Routing.RegisterRoute("details", typeof(ShellTestPage));
+
+                       shell.Items.Add(item);
+
+
+                       await shell.GoToAsync(new ShellNavigationState($"//section2/details?{nameof(ShellTestPage.SomeQueryParameter)}=1234"));
+                       await shell.GoToAsync(new ShellNavigationState($"//content?{nameof(ShellTestPage.SomeQueryParameter)}=1234"));
+                       await shell.GoToAsync(new ShellNavigationState($"//section2/details?{nameof(ShellTestPage.SomeQueryParameter)}=4321"));
+
+                       var testPage = (shell.CurrentItem.CurrentItem as IShellSectionController).PresentedPage as ShellTestPage;
+                       Assert.AreEqual("4321", testPage.SomeQueryParameter);
+               }
+
+
+               [Test]
                public async Task NavigationWithQueryStringAndNoDataTemplate()
                {
                        var shell = new Shell();
@@ -239,7 +297,6 @@ namespace Xamarin.Forms.Core.UnitTests
 
                        await shell.GoToAsync(new ShellNavigationState($"//two/tabfour/content?{nameof(ShellTestPage.SomeQueryParameter)}=1234"));
                        Assert.AreEqual("1234", (two.CurrentItem.CurrentItem.Content as ShellTestPage).SomeQueryParameter);
-
                }
 
                [Test]
index 7f0eb9e..bdca27d 100644 (file)
@@ -449,9 +449,6 @@ namespace Xamarin.Forms
 
                internal static void ApplyQueryAttributes(Element element, IDictionary<string, string> query, bool isLastItem)
                {
-                       if (query.Count == 0)
-                               return;
-
                        string prefix = "";
                        if (!isLastItem)
                        {
@@ -487,7 +484,7 @@ namespace Xamarin.Forms
                                filteredQuery.Add(key, q.Value);
                        }
 
-                       if (baseShellItem != null)
+                       if (baseShellItem is ShellContent)
                                baseShellItem.ApplyQueryAttributes(filteredQuery);
                        else if (isLastItem)
                                element.SetValue(ShellContent.QueryAttributesProperty, query);
index 0f557d0..2eb9d94 100644 (file)
@@ -46,7 +46,7 @@ namespace Xamarin.Forms
                        var shellSection = request.Request.Section;
 
                        if (shellSection == null)
-                               return Task.FromResult(true);
+                               shellSection = Items[0];
 
                        Shell.ApplyQueryAttributes(shellSection, queryData, request.Request.Content == null);
 
index 7eed4b9..be80e05 100644 (file)
@@ -72,7 +72,7 @@ namespace Xamarin.Forms
                        ShellContent shellContent = request.Request.Content;
 
                        if (shellContent == null)
-                               return Task.FromResult(true);
+                               shellContent = Items[0];
 
                        if (request.Request.GlobalRoutes.Count > 0)
                        {