admin管理员组

文章数量:1406924

I've tried a few ways to do this, mainly DynamicResource, bindings and MVVM but each time I do so my ImageButtons Background changes colors but the svg doesn't. I have tried manually setting the svg's tint color and it does change. This issue doesn't seem to be the svg file or the DynamicResource because they seem to work for everything else just fine. I'm using .NET version 9. My question is, am I doing something wrong with DynamicResource and/or is there an easier or better method to do this?

<ImageButton Grid.Row="2" Grid.Column="3" BackgroundColor="{DynamicResource SecondaryColor}" CornerRadius="0" Clicked="NavigateSettings" BorderWidth="2" BorderColor="Transparent" Source="settingsicon.svg" Padding="5" x:Name="SettingsBTN">
<ImageButton.Behaviors>
    <toolkit:IconTintColorBehavior TintColor="{DynamicResource PrimaryColor}" x:Name="SettingsBTNIcon"/>
</ImageButton.Behaviors>
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

    namespace closet_app.Behaviors{
    public class IconTintColorBehavior : Behavior<ImageButton>{
        public static readonly BindableProperty TintColorProperty =
            BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(IconTintColorBehavior), Colors.Transparent,
                propertyChanged: OnTintColorChanged);

        public Color TintColor
        {
            get => (Color)GetValue(TintColorProperty);
            set => SetValue(TintColorProperty, value);
        }

        private static void OnTintColorChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is IconTintColorBehavior behavior && behavior.AssociatedObject != null)
            {
                behavior.AssociatedObject.SetAppThemeColor(ImageButton.BackgroundColorProperty, (Color)newValue, (Color)newValue);
            }
        }

        protected override void OnAttachedTo(ImageButton bindable)
        {
            base.OnAttachedTo(bindable);
            AssociatedObject = bindable;
        }

        protected override void OnDetachingFrom(ImageButton bindable)
        {
            base.OnDetachingFrom(bindable);
            AssociatedObject = null;
        }

        private ImageButton? AssociatedObject { get; set; }
    }
}

I've tried a few ways to do this, mainly DynamicResource, bindings and MVVM but each time I do so my ImageButtons Background changes colors but the svg doesn't. I have tried manually setting the svg's tint color and it does change. This issue doesn't seem to be the svg file or the DynamicResource because they seem to work for everything else just fine. I'm using .NET version 9. My question is, am I doing something wrong with DynamicResource and/or is there an easier or better method to do this?

<ImageButton Grid.Row="2" Grid.Column="3" BackgroundColor="{DynamicResource SecondaryColor}" CornerRadius="0" Clicked="NavigateSettings" BorderWidth="2" BorderColor="Transparent" Source="settingsicon.svg" Padding="5" x:Name="SettingsBTN">
<ImageButton.Behaviors>
    <toolkit:IconTintColorBehavior TintColor="{DynamicResource PrimaryColor}" x:Name="SettingsBTNIcon"/>
</ImageButton.Behaviors>
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

    namespace closet_app.Behaviors{
    public class IconTintColorBehavior : Behavior<ImageButton>{
        public static readonly BindableProperty TintColorProperty =
            BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(IconTintColorBehavior), Colors.Transparent,
                propertyChanged: OnTintColorChanged);

        public Color TintColor
        {
            get => (Color)GetValue(TintColorProperty);
            set => SetValue(TintColorProperty, value);
        }

        private static void OnTintColorChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is IconTintColorBehavior behavior && behavior.AssociatedObject != null)
            {
                behavior.AssociatedObject.SetAppThemeColor(ImageButton.BackgroundColorProperty, (Color)newValue, (Color)newValue);
            }
        }

        protected override void OnAttachedTo(ImageButton bindable)
        {
            base.OnAttachedTo(bindable);
            AssociatedObject = bindable;
        }

        protected override void OnDetachingFrom(ImageButton bindable)
        {
            base.OnDetachingFrom(bindable);
            AssociatedObject = null;
        }

        private ImageButton? AssociatedObject { get; set; }
    }
}
Share Improve this question edited Mar 7 at 6:02 Basheer Jarrah 6404 silver badges16 bronze badges asked Mar 6 at 13:21 user29915413user29915413
Add a comment  | 

1 Answer 1

Reset to default 0

I noticed that you set TintColor as a bindable property, but in this page you defined its color through Dynamic Resource.

For the case of using Dynamic Resource, you only need to change the value in Resource to change the color.

Please refer to the following document for specific steps:

Dynamic styles.

本文标签: changing colors dynamically in net maui (changing other pages colors within a single page)Stack Overflow