Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure Fluent resources for AutoSuggestBox on WASM #1173

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using Uno.Gallery.ViewModels;
using Microsoft.UI.Xaml.Controls;
using Uno.Gallery.Helpers;
using Microsoft.UI.Xaml.Controls.Primitives;

namespace Uno.Gallery.Views.Samples
{
Expand All @@ -12,17 +14,30 @@ public sealed partial class AutoSuggestBoxSamplePage : Page
public AutoSuggestBoxSamplePage()
{
this.InitializeComponent();
Loaded += AutoSuggestBoxSamplePage_Loaded;
}

private void AutoSuggestBoxSamplePage_Loaded(object sender, RoutedEventArgs e)
{
#if HAS_UNO
var desc = VisualTreeHelperEx.GetDescendants(this).OfType<AutoSuggestBox>();

foreach (var item in desc)
{

var border = item.GetTemplateChild("SuggestionsContainer") as Border;
border?.EnsureXamlControlsResources(true);
}
#endif
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))
{
Expand All @@ -31,28 +46,25 @@ 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();
}
}

private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
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)
{
Expand Down Expand Up @@ -88,7 +100,7 @@ public class AutoSuggestBoxSamplePageViewModel : ViewModelBase

public Sample SearchBoxSelectedItem { get => GetProperty<Sample>(); set => SetProperty(value); }

public List<Sample> GetSuggestedItems(string searchQuery)
public List<Sample>? GetSuggestedItems(string searchQuery)
{
if (string.IsNullOrEmpty(searchQuery))
{
Expand Down
Loading