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
1 Answer
Reset to default 0I 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 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744973438a2635374.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论