205 строки
6.7 KiB
C#
205 строки
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Reflection;
|
|
using System.Windows.Input;
|
|
using Xamarin.Forms.Internals;
|
|
using Xamarin.Forms.Platform;
|
|
|
|
namespace Xamarin.Forms
|
|
{
|
|
[RenderWith(typeof(_ImageButtonRenderer))]
|
|
public class ImageButton : View, IImageController, IElementConfiguration<ImageButton>, IBorderElement, IButtonController, IViewController, IPaddingElement, IButtonElement, IImageElement
|
|
{
|
|
const int DefaultCornerRadius = -1;
|
|
|
|
public static readonly BindableProperty CommandProperty = ButtonElement.CommandProperty;
|
|
|
|
public static readonly BindableProperty CommandParameterProperty = ButtonElement.CommandParameterProperty;
|
|
|
|
public static readonly BindableProperty CornerRadiusProperty = BorderElement.CornerRadiusProperty;
|
|
|
|
public static readonly BindableProperty BorderWidthProperty = BorderElement.BorderWidthProperty;
|
|
|
|
public static readonly BindableProperty BorderColorProperty = BorderElement.BorderColorProperty;
|
|
|
|
public static readonly BindableProperty SourceProperty = ImageElement.SourceProperty;
|
|
|
|
public static readonly BindableProperty AspectProperty = ImageElement.AspectProperty;
|
|
|
|
public static readonly BindableProperty IsOpaqueProperty = ImageElement.IsOpaqueProperty;
|
|
|
|
internal static readonly BindablePropertyKey IsLoadingPropertyKey = BindableProperty.CreateReadOnly(nameof(IsLoading), typeof(bool), typeof(ImageButton), default(bool));
|
|
|
|
public static readonly BindableProperty IsLoadingProperty = IsLoadingPropertyKey.BindableProperty;
|
|
|
|
internal static readonly BindablePropertyKey IsPressedPropertyKey = BindableProperty.CreateReadOnly(nameof(IsPressed), typeof(bool), typeof(ImageButton), default(bool));
|
|
|
|
public static readonly BindableProperty IsPressedProperty = IsPressedPropertyKey.BindableProperty;
|
|
|
|
public static readonly BindableProperty PaddingProperty = PaddingElement.PaddingProperty;
|
|
public event EventHandler Clicked;
|
|
public event EventHandler Pressed;
|
|
public event EventHandler Released;
|
|
|
|
readonly Lazy<PlatformConfigurationRegistry<ImageButton>> _platformConfigurationRegistry;
|
|
|
|
|
|
public ImageButton()
|
|
{
|
|
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<ImageButton>>(() => new PlatformConfigurationRegistry<ImageButton>(this));
|
|
}
|
|
|
|
public Color BorderColor
|
|
{
|
|
get { return (Color)GetValue(BorderElement.BorderColorProperty); }
|
|
set { SetValue(BorderElement.BorderColorProperty, value); }
|
|
}
|
|
|
|
public int CornerRadius
|
|
{
|
|
get { return (int)GetValue(CornerRadiusProperty); }
|
|
set { SetValue(CornerRadiusProperty, value); }
|
|
}
|
|
|
|
public double BorderWidth
|
|
{
|
|
get { return (double)GetValue(BorderWidthProperty); }
|
|
set { SetValue(BorderWidthProperty, value); }
|
|
}
|
|
|
|
public Aspect Aspect
|
|
{
|
|
get { return (Aspect)GetValue(AspectProperty); }
|
|
set { SetValue(AspectProperty, value); }
|
|
}
|
|
|
|
public bool IsLoading => (bool)GetValue(IsLoadingProperty);
|
|
|
|
public bool IsPressed => (bool)GetValue(IsPressedProperty);
|
|
|
|
public bool IsOpaque
|
|
{
|
|
get { return (bool)GetValue(IsOpaqueProperty); }
|
|
set { SetValue(IsOpaqueProperty, value); }
|
|
}
|
|
public ICommand Command
|
|
{
|
|
get { return (ICommand)GetValue(CommandProperty); }
|
|
set { SetValue(CommandProperty, value); }
|
|
}
|
|
|
|
public object CommandParameter
|
|
{
|
|
get { return GetValue(CommandParameterProperty); }
|
|
set { SetValue(CommandParameterProperty, value); }
|
|
}
|
|
|
|
[TypeConverter(typeof(ImageSourceConverter))]
|
|
public ImageSource Source
|
|
{
|
|
get { return (ImageSource)GetValue(SourceProperty); }
|
|
set { SetValue(SourceProperty, value); }
|
|
}
|
|
|
|
bool IButtonElement.IsEnabledCore
|
|
{
|
|
set { SetValueCore(IsEnabledProperty, value); }
|
|
}
|
|
|
|
protected override void OnBindingContextChanged()
|
|
{
|
|
ImageElement.OnBindingContextChanged(this, this);
|
|
base.OnBindingContextChanged();
|
|
}
|
|
|
|
protected internal override void ChangeVisualState()
|
|
{
|
|
if (IsEnabled && IsPressed)
|
|
{
|
|
VisualStateManager.GoToState(this, ButtonElement.PressedVisualState);
|
|
}
|
|
else
|
|
{
|
|
base.ChangeVisualState();
|
|
}
|
|
}
|
|
|
|
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
|
|
{
|
|
SizeRequest desiredSize = base.OnMeasure(double.PositiveInfinity, double.PositiveInfinity);
|
|
return ImageElement.Measure(this, desiredSize, widthConstraint, heightConstraint);
|
|
}
|
|
|
|
public IPlatformElementConfiguration<T, ImageButton> On<T>() where T : IConfigPlatform => _platformConfigurationRegistry.Value.On<T>();
|
|
|
|
int IBorderElement.CornerRadiusDefaultValue => (int)CornerRadiusProperty.DefaultValue;
|
|
|
|
Color IBorderElement.BorderColorDefaultValue => (Color)BorderColorProperty.DefaultValue;
|
|
|
|
double IBorderElement.BorderWidthDefaultValue => (double)BorderWidthProperty.DefaultValue;
|
|
|
|
void IBorderElement.OnBorderColorPropertyChanged(Color oldValue, Color newValue)
|
|
{
|
|
}
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
public void SetIsLoading(bool isLoading) => SetValue(IsLoadingPropertyKey, isLoading);
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
public void SetIsPressed(bool isPressed) =>
|
|
SetValue(IsPressedPropertyKey, isPressed);
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
public void SendClicked() =>
|
|
ButtonElement.ElementClicked(this, this);
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
public void SendPressed() =>
|
|
ButtonElement.ElementPressed(this, this);
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
public void SendReleased() =>
|
|
ButtonElement.ElementReleased(this, this);
|
|
|
|
public void PropagateUpClicked() =>
|
|
Clicked?.Invoke(this, EventArgs.Empty);
|
|
|
|
public void PropagateUpPressed() =>
|
|
Pressed?.Invoke(this, EventArgs.Empty);
|
|
|
|
public void PropagateUpReleased() =>
|
|
Released?.Invoke(this, EventArgs.Empty);
|
|
|
|
public void RaiseImageSourcePropertyChanged() =>
|
|
OnPropertyChanged(nameof(Source));
|
|
|
|
public Thickness Padding
|
|
{
|
|
get { return (Thickness)GetValue(PaddingElement.PaddingProperty); }
|
|
set { SetValue(PaddingElement.PaddingProperty, value); }
|
|
}
|
|
|
|
Thickness IPaddingElement.PaddingDefaultValueCreator()
|
|
{
|
|
return default(Thickness);
|
|
}
|
|
|
|
void IPaddingElement.OnPaddingPropertyChanged(Thickness oldValue, Thickness newValue)
|
|
{
|
|
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
|
|
}
|
|
|
|
void IImageElement.OnImageSourcesSourceChanged(object sender, EventArgs e) =>
|
|
ImageElement.ImageSourcesSourceChanged(this, e);
|
|
|
|
void IButtonElement.OnCommandCanExecuteChanged(object sender, EventArgs e) =>
|
|
ButtonElement.CommandCanExecuteChanged(this, EventArgs.Empty);
|
|
|
|
|
|
bool IBorderElement.IsCornerRadiusSet() => IsSet(CornerRadiusProperty);
|
|
bool IBorderElement.IsBackgroundColorSet() => IsSet(BackgroundColorProperty);
|
|
bool IBorderElement.IsBorderColorSet() => IsSet(BorderColorProperty);
|
|
bool IBorderElement.IsBorderWidthSet() => IsSet(BorderWidthProperty);
|
|
}
|
|
} |