From 629d4823ae12013ef0d428116291c688bf8cb687 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Thu, 31 Jan 2019 00:00:14 +0200 Subject: [PATCH] [Control Gallery] Basic Search + Visual Selector (#5014) * Implement basic search on the gallery home page * Added a context menu so that the visual can be dynamically changed * Added sorting to the gallery --- Xamarin.Forms.Controls/CoreGallery.cs | 37 ++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/Xamarin.Forms.Controls/CoreGallery.cs b/Xamarin.Forms.Controls/CoreGallery.cs index ba379f2..2e28552 100644 --- a/Xamarin.Forms.Controls/CoreGallery.cs +++ b/Xamarin.Forms.Controls/CoreGallery.cs @@ -401,7 +401,29 @@ namespace Xamarin.Forms.Controls _pages.Insert(1, new GalleryPageFactory(() => new TitleView(true), "TitleView")); } - var template = new DataTemplate(typeof(TextCell)); + _pages.Sort((x, y) => string.Compare(x.Title, y.Title, true)); + + var template = new DataTemplate(() => + { + var cell = new TextCell(); + cell.ContextActions.Add(new MenuItem + { + Text = "Select Visual", + Command = new Command(async () => + { + var buttons = typeof(VisualMarker).GetProperties().Select(p => p.Name); + var selection = await rootPage.DisplayActionSheet("Select Visual", "Cancel", null, buttons.ToArray()); + if (cell.BindingContext is GalleryPageFactory pageFactory) + { + var page = pageFactory.Realize(); + if (typeof(VisualMarker).GetProperty(selection)?.GetValue(null) is IVisual visual) + page.Visual = visual; + await PushPage(page); + } + }) + }); + return cell; + }); template.SetBinding(TextCell.TextProperty, "Title"); BindingContext = _pages; @@ -450,6 +472,14 @@ namespace Xamarin.Forms.Controls await PushPage(page); } + + public void FilterPages(string filter) + { + if (string.IsNullOrWhiteSpace(filter)) + ItemsSource = _pages; + else + ItemsSource = _pages.Where(p => p.Title.IndexOf(filter, StringComparison.InvariantCultureIgnoreCase) != -1); + } } [Preserve(AllMembers = true)] internal class CoreRootPage : ContentPage @@ -469,6 +499,11 @@ namespace Xamarin.Forms.Controls AutomationId = "SearchBar" }; + searchBar.TextChanged += (sender, e) => + { + corePageView.FilterPages(e.NewTextValue); + }; + var testCasesButton = new Button { Text = "Go to Test Cases", -- 2.7.4