This commit is contained in:
Nathaniel Bond 2022-07-20 03:18:34 -04:00
Родитель 122b2c3066
Коммит 996336fa4f
3 изменённых файлов: 64 добавлений и 1 удалений

Просмотреть файл

@ -57,6 +57,7 @@ namespace Comet
{ typeof(FlyoutView), typeof(FlyoutViewHandler) },
{ typeof(GraphicsView), typeof(GraphicsViewHandler) },
{ typeof(Image) , typeof(ImageHandler) },
{ typeof(ImageButton) , typeof(ImageButtonHandler) },
//{ typeof(Picker), typeof(PickerHandler) },
{ typeof(ProgressBar), typeof(ProgressBarHandler) },
{ typeof(SearchBar), typeof(SearchBarHandler) },

Просмотреть файл

@ -5,7 +5,7 @@ using Microsoft.Maui;
using Comet;
//Property:NewPropertyName=DefaultValue
[assembly: CometGenerate(typeof(ITextButton), nameof(ITextButton.Text), nameof(IButton.Clicked), ClassName = "Button", Skip = new[] { $"{nameof(ITextStyle.TextColor)}:{EnvironmentKeys.Colors.Color}" }, Namespace = "Comet")]
//[assembly: CometGenerate(typeof(IImageButton), nameof(IImageButton.Source), nameof(IButton.Clicked), ClassName = "ImageButton", Namespace = "Comet")]
[assembly: CometGenerate(typeof(IImageButton), nameof(IImageButton.Source), nameof(IButton.Clicked), ClassName = "ImageButton", Namespace = "Comet")]
//[assembly: CometGenerate(typeof(IBorder), BaseClass = "ContentView", Namespace = "Comet")]
[assembly: CometGenerate(typeof(IIndicatorView), nameof(IIndicatorView.Count), ClassName = "IndicatorView", Namespace = "Comet")]

Просмотреть файл

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using Comet.Graphics;
using Microsoft.Maui;
using Microsoft.Maui.Graphics;
namespace Comet
{
public partial class ImageButton : View, Microsoft.Maui.IImageButton
{
protected static Dictionary<string, string> ImageHandlerPropertyMapper = new(HandlerPropertyMapper)
{
[nameof(ImageSource)] = nameof(IImageSourcePart.Source),
};
public ImageButton(Binding<string> source, Action clicked = null)
{
StringSource = source;
Clicked = clicked;
}
public ImageButton(Func<string> source, Action clicked = null) : this((Binding<string>)source, clicked) { }
private Binding<string> _source;
public Binding<string> StringSource
{
get => _source;
protected set
{
this.SetBindingValue(ref _source, value);
CreateImageSource(_source.CurrentValue);
}
}
public override void ViewPropertyChanged(string property, object value)
{
base.ViewPropertyChanged(property, value);
if (property == nameof(StringSource))
{
InvalidateMeasurement();
CreateImageSource((string)value);
}
}
private void CreateImageSource(string source)
{
try
{
this.source ??= new Binding<IImageSource>();
this.source.Set((ImageSource)source);
ViewHandler?.UpdateValue(nameof(IImageSourcePart.Source));
}
catch (Exception exc)
{
Logger.Warn("An unexpected error occurred loading a bitmap.", exc);
}
}
protected override string GetHandlerPropertyName(string property)
=> ImageHandlerPropertyMapper.TryGetValue(property, out var value) ? value : property;
}
}