From 5ddc55d39f705ce18535dea896d07fbbf75d83ff Mon Sep 17 00:00:00 2001 From: McNets Date: Tue, 8 Oct 2024 16:56:58 +0200 Subject: [PATCH 1/4] fix: Ensure Fluent resources for AutoSuggestBox on WASM --- .../AutoSuggestBoxSamplePage.xaml.cs | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs b/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs index 6b3a78c72..01d5230d4 100644 --- a/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs +++ b/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs @@ -3,6 +3,7 @@ using System.Linq; using Uno.Gallery.ViewModels; using Microsoft.UI.Xaml.Controls; +using Uno.Gallery.Helpers; namespace Uno.Gallery.Views.Samples { @@ -12,17 +13,23 @@ public sealed partial class AutoSuggestBoxSamplePage : Page public AutoSuggestBoxSamplePage() { this.InitializeComponent(); + Loaded += AutoSuggestBoxSamplePage_Loaded; + } + + private void AutoSuggestBoxSamplePage_Loaded(object sender, RoutedEventArgs e) + { + // set Fluent styles for the AutoSuggestBox popup. + var border = GetTemplateChild("SuggestionsContainer") as Border; + border?.EnsureXamlControlsResources(true); + Loaded -= AutoSuggestBoxSamplePage_Loaded; } private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) { - //This check can be removed when https://github.com/unoplatform/uno/issues/11635 is fixed -#if !__ANDROID__ && !__IOS__ if (args.Reason != AutoSuggestionBoxTextChangeReason.UserInput) { return; } -#endif if (string.IsNullOrEmpty(sender.Text)) { @@ -31,7 +38,7 @@ private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTex if (((Sample)DataContext).Data is AutoSuggestBoxSamplePageViewModel viewModel) { - sender.ItemsSource = viewModel.GetSuggestedItems(sender.Text).Select(sample => sample.Title).ToList(); + sender.ItemsSource = viewModel.GetSuggestedItems(sender.Text)?.Select(sample => sample.Title).ToList(); } } @@ -39,20 +46,17 @@ private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestB { if (((Sample)DataContext).Data is AutoSuggestBoxSamplePageViewModel viewModel) { - viewModel.SelectedString = args.SelectedItem.ToString(); + viewModel.SelectedString = args.SelectedItem.ToString() ?? string.Empty; } } private void SearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) { - //This check can be removed when https://github.com/unoplatform/uno/issues/11635 is fixed -#if !__ANDROID__ && !__IOS__ if (args.Reason != AutoSuggestionBoxTextChangeReason.UserInput) { return; } -#endif if (((Sample)DataContext).Data is AutoSuggestBoxSamplePageViewModel viewModel) { @@ -88,7 +92,7 @@ public class AutoSuggestBoxSamplePageViewModel : ViewModelBase public Sample SearchBoxSelectedItem { get => GetProperty(); set => SetProperty(value); } - public List GetSuggestedItems(string searchQuery) + public List? GetSuggestedItems(string searchQuery) { if (string.IsNullOrEmpty(searchQuery)) { From e08ca8288fbc28ae5a752ddf6a0ffa286a75b1e3 Mon Sep 17 00:00:00 2001 From: Joan Magnet Date: Wed, 9 Oct 2024 17:28:37 +0200 Subject: [PATCH 2/4] fix: Fluent is applied to all AutosuggestBox of the page. --- .../SamplePages/AutoSuggestBoxSamplePage.xaml.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs b/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs index 01d5230d4..9d8806e8b 100644 --- a/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs +++ b/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs @@ -18,9 +18,14 @@ public AutoSuggestBoxSamplePage() private void AutoSuggestBoxSamplePage_Loaded(object sender, RoutedEventArgs e) { - // set Fluent styles for the AutoSuggestBox popup. - var border = GetTemplateChild("SuggestionsContainer") as Border; - border?.EnsureXamlControlsResources(true); + var desc = VisualTreeHelperEx.GetDescendants(this).OfType(); + + foreach (var item in desc) + { + var border = item.GetTemplateChild("SuggestionsContainer") as Border; + border?.EnsureXamlControlsResources(true); + } + Loaded -= AutoSuggestBoxSamplePage_Loaded; } From d9ef3df1af93dddb0628839247cf301675221c06 Mon Sep 17 00:00:00 2001 From: McNets Date: Sun, 13 Oct 2024 11:30:25 +0200 Subject: [PATCH 3/4] fix: Windows is appying the proper style. GetTemplateChild on Windows uses Uno.Themes / GetTemplateChild extension class instead of Microsoft.UI.Xaml.Controls. --- .../SamplePages/AutoSuggestBoxSamplePage.xaml.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs b/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs index 9d8806e8b..886f30a6c 100644 --- a/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs +++ b/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs @@ -4,6 +4,7 @@ using Uno.Gallery.ViewModels; using Microsoft.UI.Xaml.Controls; using Uno.Gallery.Helpers; +using Microsoft.UI.Xaml.Controls.Primitives; namespace Uno.Gallery.Views.Samples { @@ -18,14 +19,22 @@ public AutoSuggestBoxSamplePage() private void AutoSuggestBoxSamplePage_Loaded(object sender, RoutedEventArgs e) { +#if HAS_UNO var desc = VisualTreeHelperEx.GetDescendants(this).OfType(); foreach (var item in desc) { + // Windows specific + //var popups = VisualTreeHelperEx.GetDescendants(item).OfType(); + //foreach (var popup in popups) + //{ + // popup.EnsureXamlControlsResources(true); + //} + var border = item.GetTemplateChild("SuggestionsContainer") as Border; border?.EnsureXamlControlsResources(true); } - +#endif Loaded -= AutoSuggestBoxSamplePage_Loaded; } From 0afbb51d50093e7bfb661f5eae4e995f161ea112 Mon Sep 17 00:00:00 2001 From: Joan Magnet Date: Sun, 13 Oct 2024 18:55:07 +0200 Subject: [PATCH 4/4] Update Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs Co-authored-by: Dominik Titl <78549750+morning4coffe-dev@users.noreply.github.com> --- .../Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs b/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs index 886f30a6c..e81ba748e 100644 --- a/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs +++ b/Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs @@ -24,12 +24,6 @@ private void AutoSuggestBoxSamplePage_Loaded(object sender, RoutedEventArgs e) foreach (var item in desc) { - // Windows specific - //var popups = VisualTreeHelperEx.GetDescendants(item).OfType(); - //foreach (var popup in popups) - //{ - // popup.EnsureXamlControlsResources(true); - //} var border = item.GetTemplateChild("SuggestionsContainer") as Border; border?.EnsureXamlControlsResources(true);