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 popups #1134

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public List<Sample> GetSuggestedItems(string searchQuery)

<StackPanel Spacing="4"
Margin="0,20,0,0"
MaxWidth="300"
Visibility="{Binding Data.SearchBoxSelectedItem, Converter={StaticResource NullToCollapsed}}">

<TextBlock Text="{Binding Data.SearchBoxSelectedItem.Title}"
Expand Down
22 changes: 16 additions & 6 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.Media;

namespace Uno.Gallery.Views.Samples
{
Expand All @@ -16,13 +18,10 @@ public AutoSuggestBoxSamplePage()

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,6 +30,7 @@ private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTex

if (((Sample)DataContext).Data is AutoSuggestBoxSamplePageViewModel viewModel)
{
EnsureFluentResourcesForPopups(sender);
sender.ItemsSource = viewModel.GetSuggestedItems(sender.Text).Select(sample => sample.Title).ToList();
}
}
Expand All @@ -46,16 +46,14 @@ private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestB

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)
{
EnsureFluentResourcesForPopups(sender);
sender.ItemsSource = viewModel.GetSuggestedItems(sender.Text);
}
}
Expand All @@ -80,6 +78,18 @@ private void SearchBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSug
viewModel.SearchBoxSelectedItem = (Sample)args.SelectedItem;
}
}

// This workaround is necessary since the app's main theme is Material and we need to ensure it displays with Fluent styles
private void EnsureFluentResourcesForPopups(AutoSuggestBox sender)
{
if (sender.IsSuggestionListOpen)
{
var popup = VisualTreeHelper.GetOpenPopupsForXamlRoot(XamlRoot).FirstOrDefault();

popup.EnsureXamlControlsResources(true);
(popup.Child as Border).EnsureXamlControlsResources(true);
}
}
}

public class AutoSuggestBoxSamplePageViewModel : ViewModelBase
Expand Down
Loading