Skip to content

Commit 238f413

Browse files
myix765bijingtonTheCodeTraveler
authored
Fix issue with tint not applying to loaded images (#2077)
* Set image source if not matching element source, allow tint to apply if image loaded * Remove check for image loaded which caused issue with ToggleImageSource button * Add check back for IsLoaded, edit check for if image is not ready to be tinted * Reset copying over TryGetButtonImage change * Changed location of matching source check * Add `OnIconTintColorBehaviorPropertyChanged` * Update IconTintColorBehavior.windows.cs --------- Co-authored-by: Shaun Lawrence <shaunrlawrence@gmail.com> Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com>
1 parent 8f41526 commit 238f413

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.windows.cs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,8 @@ protected override void OnAttachedTo(View bindable, FrameworkElement platformVie
2727

2828
ApplyTintColor(platformView, bindable, TintColor);
2929

30+
PropertyChanged += OnIconTintColorBehaviorPropertyChanged;
3031
bindable.PropertyChanged += OnElementPropertyChanged;
31-
this.PropertyChanged += (s, e) =>
32-
{
33-
if (e.PropertyName == TintColorProperty.PropertyName)
34-
{
35-
if (currentColorBrush is not null && TintColor is not null)
36-
{
37-
currentColorBrush.Color = TintColor.ToWindowsColor();
38-
}
39-
else
40-
{
41-
ApplyTintColor(platformView, bindable, TintColor);
42-
}
43-
}
44-
};
4532
}
4633

4734
/// <inheritdoc/>
@@ -50,6 +37,8 @@ protected override void OnDetachedFrom(View bindable, FrameworkElement platformV
5037
base.OnDetachedFrom(bindable, platformView);
5138

5239
bindable.PropertyChanged -= OnElementPropertyChanged;
40+
PropertyChanged -= OnIconTintColorBehaviorPropertyChanged;
41+
5342
RemoveTintColor(platformView);
5443
}
5544

@@ -83,10 +72,34 @@ static bool TryGetSourceImageUri(WImage? imageControl, IImageElement? imageEleme
8372
return false;
8473
}
8574

75+
void OnIconTintColorBehaviorPropertyChanged(object? sender, PropertyChangedEventArgs e)
76+
{
77+
ArgumentNullException.ThrowIfNull(sender);
78+
var iconTintColorBehavior = (IconTintColorBehavior)sender;
79+
80+
if (iconTintColorBehavior.View is not IView bindable
81+
|| bindable.Handler?.PlatformView is not FrameworkElement platformView)
82+
{
83+
return;
84+
}
85+
86+
if (e.PropertyName == TintColorProperty.PropertyName)
87+
{
88+
if (currentColorBrush is not null && TintColor is not null)
89+
{
90+
currentColorBrush.Color = TintColor.ToWindowsColor();
91+
}
92+
else
93+
{
94+
ApplyTintColor(platformView, bindable, TintColor);
95+
}
96+
}
97+
}
98+
8699
void OnElementPropertyChanged(object? sender, PropertyChangedEventArgs e)
87100
{
88101
if (e.PropertyName is not string propertyName
89-
|| sender is not View bindable
102+
|| sender is not IView bindable
90103
|| bindable.Handler?.PlatformView is not FrameworkElement platformView)
91104
{
92105
return;
@@ -99,7 +112,7 @@ void OnElementPropertyChanged(object? sender, PropertyChangedEventArgs e)
99112
}
100113
}
101114

102-
void ApplyTintColor(FrameworkElement platformView, View element, Color? color)
115+
void ApplyTintColor(FrameworkElement platformView, IView element, Color? color)
103116
{
104117
RemoveTintColor(platformView);
105118

@@ -128,7 +141,7 @@ void ApplyTintColor(FrameworkElement platformView, View element, Color? color)
128141
}
129142
}
130143

131-
void LoadAndApplyImageTintColor(View element, WImage image, Color color)
144+
void LoadAndApplyImageTintColor(IView element, WImage image, Color color)
132145
{
133146
if (element is IImageElement { Source: UriImageSource uriImageSource })
134147
{
@@ -140,6 +153,21 @@ void LoadAndApplyImageTintColor(View element, WImage image, Color color)
140153

141154
ApplyTintColor();
142155
}
156+
else if (image.IsLoaded)
157+
{
158+
// Sometimes WImage source doesn't match View source so the image is not ready to be tinted
159+
// We must wait for next ImageOpened event
160+
if (element is IImageElement { Source: FileImageSource fileImageSource }
161+
&& image.Source is BitmapImage bitmapImage
162+
&& Uri.Compare(new Uri($"{bitmapImage.UriSource.Scheme}:///{fileImageSource.File}"), bitmapImage.UriSource, UriComponents.Path, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) is not 0)
163+
{
164+
image.ImageOpened += OnImageOpened;
165+
}
166+
else
167+
{
168+
ApplyTintColor();
169+
}
170+
}
143171
else
144172
{
145173
image.ImageOpened += OnImageOpened;
@@ -178,7 +206,7 @@ void OnImageSizeChanged(object sender, SizeChangedEventArgs e)
178206
}
179207
}
180208

181-
void ApplyImageTintColor(View element, WImage image, Color color)
209+
void ApplyImageTintColor(IView element, WImage image, Color color)
182210
{
183211
if (!TryGetSourceImageUri(image, (IImageElement)element, out var uri))
184212
{

0 commit comments

Comments
 (0)