OneStepFurther
OneStepFurther

Reputation: 76

Changing TintColor of IconTintColorBehavior at runtime doesn't work

Hello I'm testing the new IconTintColorBehavior in CommunityToolkit.Maui 1.1.0. It works find, but only when starting the application. If I change the AppTheme during runtime, it doesn't work. Has anyone else observed this behavior?

<Image Source="Home.svg" >
    <Image.Behaviors>
        <mct:IconTintColorBehavior TintColor="{AppThemeBinding Light={StaticResource Gray950}, Dark={StaticResource White}}"/>
    </Image.Behaviors>
</Image>

Upvotes: 5

Views: 3847

Answers (3)

TouchBoarder
TouchBoarder

Reputation: 6492

To fix this I had to use a viewModel implementing INotifyPropertyChanged and bind my own updated app theme color value to the IconTintColorBehavior

private Color _AppThemeIconColor = Colors.White;
public Color AppThemeIconColor
{
  get => _AppThemeIconColor;
  set
    {
        _AppThemeIconColor = value;
        OnPropertyChanged(nameof(AppThemeIconColor));
    }
}

To update the app theme color in the viewModel I used the Application.RequestedThemeChanged event to set it.

BindingContext = viewModel = YourViewModel();

...

if(Application.Current is Application app)
 {
     app.RequestedThemeChanged += (s, a) =>
     {
         viewModel.AppThemeIconColor = a.RequestedTheme switch
         {
             AppTheme.Dark => Colors.White,
             AppTheme.Light => Colors.Black,
             AppTheme.Unspecified => Colors.White,
             _ => Colors.White
         };
     };
 }

xaml:

x:DataType="viewModels:YourViewModel"

                <Image.Behaviors>
                    <toolkit:IconTintColorBehavior TintColor="{Binding AppThemeIconColor}" />
                </Image.Behaviors>

Upvotes: 1

Alexandar May - MSFT
Alexandar May - MSFT

Reputation: 10156

Unfortunately, it could be a bug.

And I have created a new issue about this problem.

You can follow it up here:https://github.com/dotnet/maui/issues/9036.

Thank you very much for your feedback and patience!

Upvotes: 2

FreakyAli
FreakyAli

Reputation: 16562

Well if you plan on changing the value on runtime then you probably have to use DynamicResources there:

<mct:IconTintColorBehavior TintColor="{AppThemeBinding Light={DynamicResource Gray950}, Dark={DynamicResource White}}"/>

Goodluck

Upvotes: 1

Related Questions