diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/IconTintColorBehaviorPage.xaml b/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/IconTintColorBehaviorPage.xaml index 1cbdf03347..786020ac7a 100644 --- a/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/IconTintColorBehaviorPage.xaml +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/IconTintColorBehaviorPage.xaml @@ -8,8 +8,7 @@ xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Behaviors" Title="IconTintColorBehaviorPage" x:DataType="vm:IconTintColorBehaviorViewModel" - x:TypeArguments="vm:IconTintColorBehaviorViewModel" - x:Name="this"> + x:TypeArguments="vm:IconTintColorBehaviorViewModel"> + Command="{Binding ToggleImageButtonCommand, Mode=OneTime}" + Source="{Binding ToggleableImageSource}"> - + @@ -98,13 +97,13 @@ Grid.Column="2" FontSize="Micro" FontAttributes="Italic" - Text="The ImageButton above uses Binding in order to change the TintColor. Click it!"/> + Text="This ImageButton uses Bindings to change the TintColor. Click it!"/> diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/IconTintColorBehaviorPage.xaml.cs b/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/IconTintColorBehaviorPage.xaml.cs index 0aecd5e01b..23f32e1def 100644 --- a/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/IconTintColorBehaviorPage.xaml.cs +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/IconTintColorBehaviorPage.xaml.cs @@ -9,22 +9,4 @@ public IconTintColorBehaviorPage(IconTintColorBehaviorViewModel iconTintColorBeh { InitializeComponent(); } - - void ChangeSource_Clicked(System.Object sender, System.EventArgs e) - { - var imageSource = (ImageButton)sender; - var selectedImage = imageSource.Source as FileImageSource; - - if (selectedImage is not null) - { - if (selectedImage.File == "dotnet_bot.png") - { - UpdateImage.Source = "shield.png"; - } - else - { - UpdateImage.Source = "dotnet_bot.png"; - } - } - } } \ No newline at end of file diff --git a/samples/CommunityToolkit.Maui.Sample/ViewModels/Behaviors/IconTintColorBehaviorViewModel.cs b/samples/CommunityToolkit.Maui.Sample/ViewModels/Behaviors/IconTintColorBehaviorViewModel.cs index ef44b80932..37f41c82dc 100644 --- a/samples/CommunityToolkit.Maui.Sample/ViewModels/Behaviors/IconTintColorBehaviorViewModel.cs +++ b/samples/CommunityToolkit.Maui.Sample/ViewModels/Behaviors/IconTintColorBehaviorViewModel.cs @@ -5,15 +5,37 @@ namespace CommunityToolkit.Maui.Sample.ViewModels.Behaviors; public partial class IconTintColorBehaviorViewModel : BaseViewModel { + const string dotnetBotImageFileName = "dotnet_bot.png"; + const string shieldImageFileName = "shield.png"; + + static readonly IEnumerator toggleableColorsEnumerator = new List { Colors.Red, Colors.Green, null }.GetEnumerator(); + + [ObservableProperty] + string toggleableImageSource = shieldImageFileName; + + [ObservableProperty] + Color? toggleableIconTintColor = toggleableColorsEnumerator.Current; + [RelayCommand] - void Change() + void ToggleImageButton() { - IsChanged = !IsChanged; + ToggleableImageSource = ToggleableImageSource switch + { + dotnetBotImageFileName => shieldImageFileName, + shieldImageFileName => dotnetBotImageFileName, + _ => throw new NotSupportedException("Invalid image source") + }; } - [ObservableProperty] - [NotifyPropertyChangedFor(nameof(ChangedColor))] - bool isChanged; + [RelayCommand] + void ChangeColor() + { + if (!toggleableColorsEnumerator.MoveNext()) + { + toggleableColorsEnumerator.Reset(); + toggleableColorsEnumerator.MoveNext(); + } - public Color ChangedColor => IsChanged ? Colors.Red : Colors.Green; + ToggleableIconTintColor = toggleableColorsEnumerator.Current; + } } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs index 3e8c3501c2..2c4344db7b 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/IconTintColor/IconTintColorBehavior.android.cs @@ -52,7 +52,7 @@ static void SetImageViewTintColor(ImageView image, Color? color) if (color is null) { image.ClearColorFilter(); - color = Colors.Transparent; + return; } image.SetColorFilter(new PorterDuffColorFilter(color.ToPlatform(), PorterDuff.Mode.SrcIn ?? throw new InvalidOperationException("PorterDuff.Mode.SrcIn should not be null at runtime.")));