This commit is contained in:
Rui Marinho 2018-11-12 12:53:42 +00:00
Родитель c3f5f1bcc5 2f416f2735
Коммит 3c3ecfd454
22 изменённых файлов: 478 добавлений и 456 удалений

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

@ -1,11 +1,19 @@
namespace Xamarin.Forms
using System;
namespace Xamarin.Forms
{
static class BorderElement
{
public const int DefaultCornerRadius = -1;
public static readonly BindableProperty BorderColorProperty =
BindableProperty.Create("BorderColor", typeof(Color), typeof(IBorderElement), Color.Default,
BindableProperty.Create(nameof(IBorderElement.BorderColor), typeof(Color), typeof(IBorderElement), Color.Default,
propertyChanged: OnBorderColorPropertyChanged);
public static readonly BindableProperty BorderWidthProperty = BindableProperty.Create(nameof(IBorderElement.BorderWidth), typeof(double), typeof(IBorderElement), -1d);
public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(IBorderElement.CornerRadius), typeof(int), typeof(IBorderElement), defaultValue: DefaultCornerRadius);
static void OnBorderColorPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
((IBorderElement)bindable).OnBorderColorPropertyChanged((Color)oldValue, (Color)newValue);

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

@ -8,16 +8,14 @@ using Xamarin.Forms.Platform;
namespace Xamarin.Forms
{
[RenderWith(typeof(_ButtonRenderer))]
public class Button : View, IFontElement, ITextElement, IBorderElement, IButtonController, IElementConfiguration<Button>, IPaddingElement, IBorderController, IImageController, IViewController
public class Button : View, IFontElement, ITextElement, IBorderElement, IButtonController, IElementConfiguration<Button>, IPaddingElement, IImageController, IViewController, IButtonElement, IImageElement
{
const double DefaultSpacing = 10;
const int DefaultBorderRadius = 5;
const int DefaultCornerRadius = -1;
const double DefaultSpacing = 10;
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(Button), null, propertyChanging: OnCommandChanging, propertyChanged: OnCommandChanged);
public static readonly BindableProperty CommandProperty = ButtonElement.CommandProperty;
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(Button), null,
propertyChanged: (bindable, oldvalue, newvalue) => ButtonElementManager.CommandCanExecuteChanged(bindable, EventArgs.Empty));
public static readonly BindableProperty CommandParameterProperty = ButtonElement.CommandParameterProperty;
public static readonly BindableProperty ContentLayoutProperty =
BindableProperty.Create("ContentLayout", typeof(ButtonContentLayout), typeof(Button), new ButtonContentLayout(ButtonContentLayout.ImagePosition.Left, DefaultSpacing));
@ -43,12 +41,10 @@ namespace Xamarin.Forms
public static readonly BindableProperty BorderRadiusProperty = BindableProperty.Create("BorderRadius", typeof(int), typeof(Button), defaultValue: DefaultBorderRadius,
propertyChanged: BorderRadiusPropertyChanged);
public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create("CornerRadius", typeof(int), typeof(Button), defaultValue: DefaultCornerRadius,
public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create("CornerRadius", typeof(int), typeof(Button), defaultValue: BorderElement.DefaultCornerRadius,
propertyChanged: CornerRadiusPropertyChanged);
public static readonly BindableProperty ImageProperty = BindableProperty.Create(nameof(Image), typeof(FileImageSource), typeof(Button), default(FileImageSource),
propertyChanging: OnImageSourceChanging,
propertyChanged: OnImageSourceChanged);
public static readonly BindableProperty ImageProperty = ImageElement.FileImageProperty;
public static readonly BindableProperty PaddingProperty = PaddingElement.PaddingProperty;
@ -143,33 +139,33 @@ namespace Xamarin.Forms
set { SetValue(TextElement.TextColorProperty, value); }
}
bool IButtonController.IsEnabledCore
bool IButtonElement.IsEnabledCore
{
set { SetValueCore(IsEnabledProperty, value); }
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendClicked() => ButtonElementManager.ElementClicked(this, this);
public void SendClicked() => ButtonElement.ElementClicked(this, this);
public bool IsPressed => (bool)GetValue(IsPressedProperty);
[EditorBrowsable(EditorBrowsableState.Never)]
void IButtonController.SetIsPressed(bool isPressed) => SetValue(IsPressedPropertyKey, isPressed);
void IButtonElement.SetIsPressed(bool isPressed) => SetValue(IsPressedPropertyKey, isPressed);
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendPressed() => ButtonElementManager.ElementPressed(this, this);
public void SendPressed() => ButtonElement.ElementPressed(this, this);
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendReleased() => ButtonElementManager.ElementReleased(this, this);
public void SendReleased() => ButtonElement.ElementReleased(this, this);
[EditorBrowsable(EditorBrowsableState.Never)]
void IButtonController.PropagateUpClicked() => Clicked?.Invoke(this, EventArgs.Empty);
void IButtonElement.PropagateUpClicked() => Clicked?.Invoke(this, EventArgs.Empty);
[EditorBrowsable(EditorBrowsableState.Never)]
void IButtonController.PropagateUpPressed() => Pressed?.Invoke(this, EventArgs.Empty);
void IButtonElement.PropagateUpPressed() => Pressed?.Invoke(this, EventArgs.Empty);
[EditorBrowsable(EditorBrowsableState.Never)]
void IButtonController.PropagateUpReleased() => Released?.Invoke(this, EventArgs.Empty);
void IButtonElement.PropagateUpReleased() => Released?.Invoke(this, EventArgs.Empty);
public FontAttributes FontAttributes
{
@ -191,10 +187,6 @@ namespace Xamarin.Forms
}
public event EventHandler Clicked;
BindableProperty IBorderController.CornerRadiusProperty => Button.CornerRadiusProperty;
BindableProperty IBorderController.BorderColorProperty => Button.BorderColorProperty;
BindableProperty IBorderController.BorderWidthProperty => Button.BorderWidthProperty;
public event EventHandler Pressed;
public event EventHandler Released;
@ -213,7 +205,7 @@ namespace Xamarin.Forms
{
if (IsEnabled && IsPressed)
{
VisualStateManager.GoToState(this, ButtonElementManager.PressedVisualState);
VisualStateManager.GoToState(this, ButtonElement.PressedVisualState);
}
else
{
@ -245,17 +237,18 @@ namespace Xamarin.Forms
void IFontElement.OnFontChanged(Font oldValue, Font newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
Aspect IImageController.Aspect => Aspect.AspectFit;
ImageSource IImageController.Source => Image;
bool IImageController.IsOpaque => false;
BindableProperty IImageController.SourceProperty => ImageProperty;
BindableProperty IImageController.AspectProperty => null;
BindableProperty IImageController.IsOpaqueProperty => null;
Aspect IImageElement.Aspect => Aspect.AspectFit;
ImageSource IImageElement.Source => Image;
bool IImageElement.IsOpaque => false;
void IImageController.RaiseImageSourcePropertyChanged() => OnPropertyChanged(ImageProperty.PropertyName);
void IImageElement.RaiseImageSourcePropertyChanged() => OnPropertyChanged(ImageProperty.PropertyName);
int IBorderElement.CornerRadiusDefaultValue => (int)CornerRadiusProperty.DefaultValue;
Color IBorderElement.BorderColorDefaultValue => (Color)BorderColorProperty.DefaultValue;
double IBorderElement.BorderWidthDefaultValue => (double)BorderWidthProperty.DefaultValue;
/// <summary>
/// Flag to prevent overwriting the value of CornerRadius
@ -270,7 +263,7 @@ namespace Xamarin.Forms
var button = (Button)bindable;
var val = (int)newvalue;
if (val == DefaultBorderRadius && !button.cornerOrBorderRadiusSetting)
val = DefaultCornerRadius;
val = BorderElement.DefaultCornerRadius;
var oldVal = (int)bindable.GetValue(Button.CornerRadiusProperty);
@ -289,7 +282,7 @@ namespace Xamarin.Forms
var button = (Button)bindable;
var val = (int)newvalue;
if (val == DefaultCornerRadius && !button.cornerOrBorderRadiusSetting)
if (val == BorderElement.DefaultCornerRadius && !button.cornerOrBorderRadiusSetting)
val = DefaultBorderRadius;
#pragma warning disable 0618 // retain until BorderRadiusProperty removed
@ -314,58 +307,21 @@ namespace Xamarin.Forms
{
}
void OnImageSourcesSourceChanged(object sender, EventArgs e) =>
ImageElementManager.ImageSourcesSourceChanged(this, EventArgs.Empty);
void IImageElement.OnImageSourcesSourceChanged(object sender, EventArgs e) =>
ImageElement.ImageSourcesSourceChanged(this, e);
static void OnImageSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
ImageSource newSource = (ImageSource)newValue;
Button button = (Button)bindable;
if (newSource != null)
{
newSource.SourceChanged += button.OnImageSourcesSourceChanged;
}
ImageElementManager.ImageSourceChanged(bindable, newSource);
}
static void OnImageSourceChanging(BindableObject bindable, object oldValue, object newValue)
{
ImageSource oldSource = (ImageSource)oldValue;
Button button = (Button)bindable;
if (oldSource != null)
{
oldSource.SourceChanged -= button.OnImageSourcesSourceChanged;
}
ImageElementManager.ImageSourceChanging(oldSource);
}
void OnCommandCanExecuteChanged(object sender, EventArgs e) =>
ButtonElementManager.CommandCanExecuteChanged(this, EventArgs.Empty);
static void OnCommandChanged(BindableObject bo, object o, object n)
{
var button = (Button)bo;
if (n is ICommand newCommand)
newCommand.CanExecuteChanged += button.OnCommandCanExecuteChanged;
ButtonElementManager.CommandChanged(button);
}
static void OnCommandChanging(BindableObject bo, object o, object n)
{
var button = (Button)bo;
if (o != null)
{
(o as ICommand).CanExecuteChanged -= button.OnCommandCanExecuteChanged;
}
}
void IButtonElement.OnCommandCanExecuteChanged(object sender, EventArgs e) =>
ButtonElement.CommandCanExecuteChanged(this, EventArgs.Empty);
void IImageController.SetIsLoading(bool isLoading)
{
}
bool IBorderElement.IsCornerRadiusSet() => IsSet(CornerRadiusProperty);
bool IBorderElement.IsBackgroundColorSet() => IsSet(BackgroundColorProperty);
bool IBorderElement.IsBorderColorSet() => IsSet(BorderColorProperty);
bool IBorderElement.IsBorderWidthSet() => IsSet(BorderWidthProperty);
[DebuggerDisplay("Image Position = {Position}, Spacing = {Spacing}")]
[TypeConverter(typeof(ButtonContentTypeConverter))]
public sealed class ButtonContentLayout

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

@ -0,0 +1,86 @@
using System;
using System.Windows.Input;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms
{
static class ButtonElement
{
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(IButtonElement.Command), typeof(ICommand), typeof(IButtonElement), null, propertyChanging: OnCommandChanging, propertyChanged: OnCommandChanged);
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(IButtonElement.CommandParameter), typeof(object), typeof(IButtonElement), null,
propertyChanged: (bindable, oldvalue, newvalue) => CommandCanExecuteChanged(bindable, EventArgs.Empty));
static void OnCommandChanged(BindableObject bo, object o, object n)
{
IButtonElement button = (IButtonElement)bo;
if (n is ICommand newCommand)
newCommand.CanExecuteChanged += button.OnCommandCanExecuteChanged;
CommandChanged(button);
}
static void OnCommandChanging(BindableObject bo, object o, object n)
{
IButtonElement button = (IButtonElement)bo;
if (o != null)
{
(o as ICommand).CanExecuteChanged -= button.OnCommandCanExecuteChanged;
}
}
public const string PressedVisualState = "Pressed";
public static void CommandChanged(IButtonElement sender)
{
if (sender.Command != null)
{
CommandCanExecuteChanged(sender, EventArgs.Empty);
}
else
{
sender.IsEnabledCore = true;
}
}
public static void CommandCanExecuteChanged(object sender, EventArgs e)
{
IButtonElement ButtonElementManager = (IButtonElement)sender;
ICommand cmd = ButtonElementManager.Command;
if (cmd != null)
{
ButtonElementManager.IsEnabledCore = cmd.CanExecute(ButtonElementManager.CommandParameter);
}
}
public static void ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager)
{
if (visualElement.IsEnabled == true)
{
ButtonElementManager.Command?.Execute(ButtonElementManager.CommandParameter);
ButtonElementManager.PropagateUpClicked();
}
}
public static void ElementPressed(VisualElement visualElement, IButtonElement ButtonElementManager)
{
if (visualElement.IsEnabled == true)
{
ButtonElementManager.SetIsPressed(true);
visualElement.ChangeVisualStateInternal();
ButtonElementManager.PropagateUpPressed();
}
}
public static void ElementReleased(VisualElement visualElement, IButtonElement ButtonElementManager)
{
if (visualElement.IsEnabled == true)
{
IButtonController buttonController = ButtonElementManager as IButtonController;
ButtonElementManager.SetIsPressed(false);
visualElement.ChangeVisualStateInternal();
ButtonElementManager.PropagateUpReleased();
}
}
}
}

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

@ -1,66 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
namespace Xamarin.Forms
{
internal static class ButtonElementManager
{
public const string PressedVisualState = "Pressed";
public static void CommandChanged(IButtonController sender)
{
if (sender.Command != null)
{
CommandCanExecuteChanged(sender, EventArgs.Empty);
}
else
{
sender.IsEnabledCore = true;
}
}
public static void CommandCanExecuteChanged(object sender, EventArgs e)
{
IButtonController ButtonElementManager = (IButtonController)sender;
ICommand cmd = ButtonElementManager.Command;
if (cmd != null)
{
ButtonElementManager.IsEnabledCore = cmd.CanExecute(ButtonElementManager.CommandParameter);
}
}
public static void ElementClicked(VisualElement visualElement, IButtonController ButtonElementManager)
{
if (visualElement.IsEnabled == true)
{
ButtonElementManager.Command?.Execute(ButtonElementManager.CommandParameter);
ButtonElementManager.PropagateUpClicked();
}
}
public static void ElementPressed(VisualElement visualElement, IButtonController ButtonElementManager)
{
if (visualElement.IsEnabled == true)
{
ButtonElementManager.SetIsPressed(true);
visualElement.ChangeVisualStateInternal();
ButtonElementManager.PropagateUpPressed();
}
}
public static void ElementReleased(VisualElement visualElement, IButtonController ButtonElementManager)
{
if (visualElement.IsEnabled == true)
{
ButtonElementManager.SetIsPressed(false);
visualElement.ChangeVisualStateInternal();
ButtonElementManager.PropagateUpReleased();
}
}
}
}

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

@ -54,6 +54,17 @@ namespace Xamarin.Forms
set { SetValue(CornerRadiusProperty, value); }
}
int IBorderElement.CornerRadius => (int)CornerRadius;
// not currently used by frame
double IBorderElement.BorderWidth => -1d;
int IBorderElement.CornerRadiusDefaultValue => (int)CornerRadiusProperty.DefaultValue;
Color IBorderElement.BorderColorDefaultValue => (Color)BorderColorProperty.DefaultValue;
double IBorderElement.BorderWidthDefaultValue => -1d;
public IPlatformElementConfiguration<T, Frame> On<T>() where T : IConfigPlatform
{
return _platformConfigurationRegistry.Value.On<T>();
@ -65,5 +76,13 @@ namespace Xamarin.Forms
OnPropertyChanged(nameof(OutlineColor));
#pragma warning restore
}
bool IBorderElement.IsCornerRadiusSet() => IsSet(CornerRadiusProperty);
bool IBorderElement.IsBackgroundColorSet() => IsSet(BackgroundColorProperty);
bool IBorderElement.IsBorderColorSet() => IsSet(BorderColorProperty);
bool IBorderElement.IsBorderWidthSet() => false;
}
}

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

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace Xamarin.Forms
{
public interface IBorderController : INotifyPropertyChanged
{
BindableProperty CornerRadiusProperty { get; }
BindableProperty BorderColorProperty { get; }
BindableProperty BorderWidthProperty { get; }
int CornerRadius { get; }
Color BorderColor { get; }
Color BackgroundColor { get; }
double BorderWidth { get; }
bool IsSet(BindableProperty targetProperty);
}
}

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

@ -1,11 +1,24 @@
namespace Xamarin.Forms
using System.ComponentModel;
namespace Xamarin.Forms
{
interface IBorderElement
[EditorBrowsable(EditorBrowsableState.Never)]
public interface IBorderElement
{
//note to implementor: implement this property publicly
Color BorderColor { get; }
int CornerRadius { get; }
Color BackgroundColor { get; }
double BorderWidth { get; }
//note to implementor: but implement the methods explicitly
void OnBorderColorPropertyChanged(Color oldValue, Color newValue);
bool IsCornerRadiusSet();
bool IsBackgroundColorSet();
bool IsBorderColorSet();
bool IsBorderWidthSet();
int CornerRadiusDefaultValue { get; }
Color BorderColorDefaultValue { get; }
double BorderWidthDefaultValue { get; }
}
}

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

@ -8,14 +8,5 @@ namespace Xamarin.Forms
void SendClicked();
void SendPressed();
void SendReleased();
object CommandParameter { get; set; }
ICommand Command { get; set; }
bool IsEnabledCore { set; }
void PropagateUpClicked();
void PropagateUpPressed();
void PropagateUpReleased();
bool IsPressed { get; }
void SetIsPressed(bool isPressed);
}
}

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

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Input;
namespace Xamarin.Forms.Internals
{
[EditorBrowsable(EditorBrowsableState.Never)]
interface IButtonElement
{
//note to implementor: implement this property publicly
object CommandParameter { get; set; }
ICommand Command { get; set; }
bool IsPressed { get; }
//note to implementor: but implement these methods explicitly
void PropagateUpClicked();
void PropagateUpPressed();
void PropagateUpReleased();
void SetIsPressed(bool isPressed);
void OnCommandCanExecuteChanged(object sender, EventArgs e);
bool IsEnabledCore { set; }
}
}

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

@ -5,12 +5,5 @@ namespace Xamarin.Forms
public interface IImageController : IViewController
{
void SetIsLoading(bool isLoading);
Aspect Aspect { get; }
ImageSource Source { get; }
bool IsOpaque { get; }
void RaiseImageSourcePropertyChanged();
BindableProperty SourceProperty { get; }
BindableProperty AspectProperty { get; }
BindableProperty IsOpaqueProperty { get; }
}
}

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

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace Xamarin.Forms
{
[EditorBrowsable(EditorBrowsableState.Never)]
public interface IImageElement
{
//note to implementor: implement this property publicly
Aspect Aspect { get; }
ImageSource Source { get; }
bool IsOpaque { get; }
//note to implementor: but implement these methods explicitly
void RaiseImageSourcePropertyChanged();
void OnImageSourcesSourceChanged(object sender, EventArgs e);
}
}

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

@ -8,14 +8,13 @@ using Xamarin.Forms.Platform;
namespace Xamarin.Forms
{
[RenderWith(typeof(_ImageRenderer))]
public class Image : View, IImageController, IElementConfiguration<Image>, IViewController
public class Image : View, IImageController, IElementConfiguration<Image>, IViewController, IImageElement
{
public static readonly BindableProperty SourceProperty = BindableProperty.Create(nameof(Source), typeof(ImageSource), typeof(Image), default(ImageSource),
propertyChanging: OnImageSourceChanging, propertyChanged: OnImageSourceChanged);
public static readonly BindableProperty SourceProperty = ImageElement.SourceProperty;
public static readonly BindableProperty AspectProperty = BindableProperty.Create(nameof(Aspect), typeof(Aspect), typeof(Image), Aspect.AspectFit);
public static readonly BindableProperty AspectProperty = ImageElement.AspectProperty;
public static readonly BindableProperty IsOpaqueProperty = BindableProperty.Create(nameof(IsOpaque), typeof(bool), typeof(Image), false);
public static readonly BindableProperty IsOpaqueProperty = ImageElement.IsOpaqueProperty;
internal static readonly BindablePropertyKey IsLoadingPropertyKey = BindableProperty.CreateReadOnly(nameof(IsLoading), typeof(bool), typeof(Image), default(bool));
@ -54,7 +53,7 @@ namespace Xamarin.Forms
protected override void OnBindingContextChanged()
{
ImageElementManager.OnBindingContextChanged(this, this);
ImageElement.OnBindingContextChanged(this, this);
base.OnBindingContextChanged();
}
@ -62,7 +61,7 @@ namespace Xamarin.Forms
protected override SizeRequest OnSizeRequest(double widthConstraint, double heightConstraint)
{
SizeRequest desiredSize = base.OnSizeRequest(double.PositiveInfinity, double.PositiveInfinity);
return ImageElementManager.Measure(this, desiredSize, widthConstraint, heightConstraint);
return ImageElement.Measure(this, desiredSize, widthConstraint, heightConstraint);
}
[EditorBrowsable(EditorBrowsableState.Never)]
@ -76,36 +75,9 @@ namespace Xamarin.Forms
return _platformConfigurationRegistry.Value.On<T>();
}
BindableProperty IImageController.SourceProperty => SourceProperty;
BindableProperty IImageController.AspectProperty => AspectProperty;
BindableProperty IImageController.IsOpaqueProperty => IsOpaqueProperty;
void IImageElement.OnImageSourcesSourceChanged(object sender, EventArgs e) =>
ImageElement.ImageSourcesSourceChanged(this, e);
void OnImageSourcesSourceChanged(object sender, EventArgs e) =>
ImageElementManager.ImageSourcesSourceChanged(this, EventArgs.Empty);
static void OnImageSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
ImageSource newSource = (ImageSource)newValue;
Image image = (Image)bindable;
if (newSource != null)
{
newSource.SourceChanged += image.OnImageSourcesSourceChanged;
}
ImageElementManager.ImageSourceChanged(bindable, newSource);
}
static void OnImageSourceChanging(BindableObject bindable, object oldValue, object newValue)
{
ImageSource oldSource = (ImageSource)oldValue;
Image image = (Image)bindable;
if (oldSource != null)
{
oldSource.SourceChanged -= image.OnImageSourcesSourceChanged;
}
ImageElementManager.ImageSourceChanging(oldSource);
}
void IImageController.RaiseImageSourcePropertyChanged() => OnPropertyChanged(nameof(Source));
void IImageElement.RaiseImageSourcePropertyChanged() => OnPropertyChanged(nameof(Source));
}
}

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

@ -9,27 +9,25 @@ using Xamarin.Forms.Platform;
namespace Xamarin.Forms
{
[RenderWith(typeof(_ImageButtonRenderer))]
public class ImageButton : View, IImageController, IElementConfiguration<ImageButton>, IBorderElement, IButtonController, IBorderController, IViewController, IPaddingElement
public class ImageButton : View, IImageController, IElementConfiguration<ImageButton>, IBorderElement, IButtonController, IViewController, IPaddingElement, IButtonElement, IImageElement
{
const int DefaultCornerRadius = -1;
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(Button), null, propertyChanging: OnCommandChanging, propertyChanged: OnCommandChanged);
public static readonly BindableProperty CommandProperty = ButtonElement.CommandProperty;
public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(int), typeof(Button), defaultValue: DefaultCornerRadius);
public static readonly BindableProperty CommandParameterProperty = ButtonElement.CommandParameterProperty;
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(ImageButton), null,
propertyChanged: (bindable, oldvalue, newvalue) => ButtonElementManager.CommandCanExecuteChanged(bindable, EventArgs.Empty));
public static readonly BindableProperty CornerRadiusProperty = BorderElement.CornerRadiusProperty;
public static readonly BindableProperty BorderWidthProperty = BindableProperty.Create(nameof(BorderWidth), typeof(double), typeof(Button), -1d);
public static readonly BindableProperty BorderWidthProperty = BorderElement.BorderWidthProperty;
public static readonly BindableProperty BorderColorProperty = BorderElement.BorderColorProperty;
public static readonly BindableProperty SourceProperty = BindableProperty.Create(nameof(Source), typeof(ImageSource), typeof(ImageButton), default(ImageSource),
propertyChanging: OnImageSourceChanging, propertyChanged: OnImageSourceChanged);
public static readonly BindableProperty SourceProperty = ImageElement.SourceProperty;
public static readonly BindableProperty AspectProperty = BindableProperty.Create(nameof(Aspect), typeof(Aspect), typeof(ImageButton), Aspect.AspectFit);
public static readonly BindableProperty AspectProperty = ImageElement.AspectProperty;
public static readonly BindableProperty IsOpaqueProperty = BindableProperty.Create(nameof(IsOpaque), typeof(bool), typeof(ImageButton), false);
public static readonly BindableProperty IsOpaqueProperty = ImageElement.IsOpaqueProperty;
internal static readonly BindablePropertyKey IsLoadingPropertyKey = BindableProperty.CreateReadOnly(nameof(IsLoading), typeof(bool), typeof(ImageButton), default(bool));
@ -104,14 +102,14 @@ namespace Xamarin.Forms
set { SetValue(SourceProperty, value); }
}
bool IButtonController.IsEnabledCore
bool IButtonElement.IsEnabledCore
{
set { SetValueCore(IsEnabledProperty, value); }
}
protected override void OnBindingContextChanged()
{
ImageElementManager.OnBindingContextChanged(this, this);
ImageElement.OnBindingContextChanged(this, this);
base.OnBindingContextChanged();
}
@ -119,7 +117,7 @@ namespace Xamarin.Forms
{
if (IsEnabled && IsPressed)
{
VisualStateManager.GoToState(this, ButtonElementManager.PressedVisualState);
VisualStateManager.GoToState(this, ButtonElement.PressedVisualState);
}
else
{
@ -130,11 +128,17 @@ namespace Xamarin.Forms
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
SizeRequest desiredSize = base.OnMeasure(double.PositiveInfinity, double.PositiveInfinity);
return ImageElementManager.Measure(this, desiredSize, widthConstraint, heightConstraint);
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)
{
}
@ -148,15 +152,15 @@ namespace Xamarin.Forms
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendClicked() =>
ButtonElementManager.ElementClicked(this, this);
ButtonElement.ElementClicked(this, this);
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendPressed() =>
ButtonElementManager.ElementPressed(this, this);
ButtonElement.ElementPressed(this, this);
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendReleased() =>
ButtonElementManager.ElementReleased(this, this);
ButtonElement.ElementReleased(this, this);
public void PropagateUpClicked() =>
Clicked?.Invoke(this, EventArgs.Empty);
@ -185,63 +189,17 @@ namespace Xamarin.Forms
{
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}
BindableProperty IBorderController.CornerRadiusProperty => ImageButton.CornerRadiusProperty;
BindableProperty IBorderController.BorderColorProperty => ImageButton.BorderColorProperty;
void IImageElement.OnImageSourcesSourceChanged(object sender, EventArgs e) =>
ImageElement.ImageSourcesSourceChanged(this, e);
BindableProperty IBorderController.BorderWidthProperty => ImageButton.BorderWidthProperty;
void IButtonElement.OnCommandCanExecuteChanged(object sender, EventArgs e) =>
ButtonElement.CommandCanExecuteChanged(this, EventArgs.Empty);
BindableProperty IImageController.SourceProperty => SourceProperty;
BindableProperty IImageController.AspectProperty => AspectProperty;
BindableProperty IImageController.IsOpaqueProperty => IsOpaqueProperty;
void OnImageSourcesSourceChanged(object sender, EventArgs e) =>
ImageElementManager.ImageSourcesSourceChanged(this, EventArgs.Empty);
static void OnImageSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
ImageSource newSource = (ImageSource)newValue;
ImageButton button = (ImageButton)bindable;
if (newSource != null)
{
newSource.SourceChanged += button.OnImageSourcesSourceChanged;
}
ImageElementManager.ImageSourceChanged(bindable, newSource);
}
static void OnImageSourceChanging(BindableObject bindable, object oldValue, object newValue)
{
ImageSource oldSource = (ImageSource)oldValue;
ImageButton button = (ImageButton)bindable;
if (oldSource != null)
{
oldSource.SourceChanged -= button.OnImageSourcesSourceChanged;
}
ImageElementManager.ImageSourceChanging(oldSource);
}
void OnCommandCanExecuteChanged(object sender, EventArgs e) =>
ButtonElementManager.CommandCanExecuteChanged(this, EventArgs.Empty);
static void OnCommandChanged(BindableObject bo, object o, object n)
{
var button = (ImageButton)bo;
if (n is ICommand newCommand)
newCommand.CanExecuteChanged += button.OnCommandCanExecuteChanged;
ButtonElementManager.CommandChanged(button);
}
static void OnCommandChanging(BindableObject bo, object o, object n)
{
var button = (ImageButton)bo;
if (o != null)
{
(o as ICommand).CanExecuteChanged -= button.OnCommandCanExecuteChanged;
}
}
bool IBorderElement.IsCornerRadiusSet() => IsSet(CornerRadiusProperty);
bool IBorderElement.IsBackgroundColorSet() => IsSet(BackgroundColorProperty);
bool IBorderElement.IsBorderColorSet() => IsSet(BorderColorProperty);
bool IBorderElement.IsBorderWidthSet() => IsSet(BorderWidthProperty);
}
}

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

@ -0,0 +1,135 @@
using System;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms
{
static class ImageElement
{
public static readonly BindableProperty FileImageProperty = BindableProperty.Create("Image", typeof(FileImageSource), typeof(IImageElement), default(FileImageSource),
propertyChanging: OnImageSourceChanging, propertyChanged: OnImageSourceChanged);
public static readonly BindableProperty SourceProperty = BindableProperty.Create(nameof(IImageElement.Source), typeof(ImageSource), typeof(IImageElement), default(ImageSource),
propertyChanging: OnImageSourceChanging, propertyChanged: OnImageSourceChanged);
public static readonly BindableProperty AspectProperty = BindableProperty.Create(nameof(IImageElement.Aspect), typeof(Aspect), typeof(IImageElement), Aspect.AspectFit);
public static readonly BindableProperty IsOpaqueProperty = BindableProperty.Create(nameof(IImageElement.IsOpaque), typeof(bool), typeof(IImageElement), false);
static void OnImageSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
ImageSource newSource = (ImageSource)newValue;
IImageElement image = (IImageElement)bindable;
if (newSource != null && image != null)
{
newSource.SourceChanged += image.OnImageSourcesSourceChanged;
}
ImageSourceChanged(bindable, newSource);
}
static void OnImageSourceChanging(BindableObject bindable, object oldValue, object newValue)
{
ImageSource oldSource = (ImageSource)oldValue;
IImageElement image = (IImageElement)bindable;
if (oldSource != null && image != null)
{
oldSource.SourceChanged -= image.OnImageSourcesSourceChanged;
}
ImageSourceChanging(oldSource);
}
public static SizeRequest Measure(IImageElement ImageElementManager, SizeRequest desiredSize, double widthConstraint, double heightConstraint)
{
double desiredAspect = desiredSize.Request.Width / desiredSize.Request.Height;
double constraintAspect = widthConstraint / heightConstraint;
double desiredWidth = desiredSize.Request.Width;
double desiredHeight = desiredSize.Request.Height;
if (desiredWidth == 0 || desiredHeight == 0)
return new SizeRequest(new Size(0, 0));
double width = desiredWidth;
double height = desiredHeight;
if (constraintAspect > desiredAspect)
{
// constraint area is proportionally wider than image
switch (ImageElementManager.Aspect)
{
case Aspect.AspectFit:
case Aspect.AspectFill:
height = Math.Min(desiredHeight, heightConstraint);
width = desiredWidth * (height / desiredHeight);
break;
case Aspect.Fill:
width = Math.Min(desiredWidth, widthConstraint);
height = desiredHeight * (width / desiredWidth);
break;
}
}
else if (constraintAspect < desiredAspect)
{
// constraint area is proportionally taller than image
switch (ImageElementManager.Aspect)
{
case Aspect.AspectFit:
case Aspect.AspectFill:
width = Math.Min(desiredWidth, widthConstraint);
height = desiredHeight * (width / desiredWidth);
break;
case Aspect.Fill:
height = Math.Min(desiredHeight, heightConstraint);
width = desiredWidth * (height / desiredHeight);
break;
}
}
else
{
// constraint area is same aspect as image
width = Math.Min(desiredWidth, widthConstraint);
height = desiredHeight * (width / desiredWidth);
}
return new SizeRequest(new Size(width, height));
}
public static void OnBindingContextChanged(IImageElement image, VisualElement visualElement)
{
if (image.Source != null)
BindableObject.SetInheritedBindingContext(image.Source, visualElement?.BindingContext);
}
public static async void ImageSourceChanging(ImageSource oldImageSource)
{
if (oldImageSource == null) return;
try
{
await oldImageSource.Cancel().ConfigureAwait(false);
}
catch (ObjectDisposedException)
{
// Workaround bugzilla 37792 https://bugzilla.xamarin.com/show_bug.cgi?id=37792
}
}
public static void ImageSourceChanged(BindableObject bindable, ImageSource newSource)
{
var visualElement = (VisualElement)bindable;
if (newSource != null)
BindableObject.SetInheritedBindingContext(newSource, visualElement?.BindingContext);
visualElement?.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}
public static void ImageSourcesSourceChanged(object sender, EventArgs e)
{
if (sender is IImageElement imageController)
imageController.RaiseImageSourcePropertyChanged();
((VisualElement)sender).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}
}
}

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

@ -6,98 +6,6 @@ namespace Xamarin.Forms
internal static class ImageElementManager
{
public static SizeRequest Measure(
IImageController ImageElementManager,
SizeRequest desiredSize,
double widthConstraint,
double heightConstraint)
{
double desiredAspect = desiredSize.Request.Width / desiredSize.Request.Height;
double constraintAspect = widthConstraint / heightConstraint;
double desiredWidth = desiredSize.Request.Width;
double desiredHeight = desiredSize.Request.Height;
if (desiredWidth == 0 || desiredHeight == 0)
return new SizeRequest(new Size(0, 0));
double width = desiredWidth;
double height = desiredHeight;
if (constraintAspect > desiredAspect)
{
// constraint area is proportionally wider than image
switch (ImageElementManager.Aspect)
{
case Aspect.AspectFit:
case Aspect.AspectFill:
height = Math.Min(desiredHeight, heightConstraint);
width = desiredWidth * (height / desiredHeight);
break;
case Aspect.Fill:
width = Math.Min(desiredWidth, widthConstraint);
height = desiredHeight * (width / desiredWidth);
break;
}
}
else if (constraintAspect < desiredAspect)
{
// constraint area is proportionally taller than image
switch (ImageElementManager.Aspect)
{
case Aspect.AspectFit:
case Aspect.AspectFill:
width = Math.Min(desiredWidth, widthConstraint);
height = desiredHeight * (width / desiredWidth);
break;
case Aspect.Fill:
height = Math.Min(desiredHeight, heightConstraint);
width = desiredWidth * (height / desiredHeight);
break;
}
}
else
{
// constraint area is same aspect as image
width = Math.Min(desiredWidth, widthConstraint);
height = desiredHeight * (width / desiredWidth);
}
return new SizeRequest(new Size(width, height));
}
internal static void OnBindingContextChanged(IImageController image, VisualElement visualElement)
{
if (image.Source != null)
BindableObject.SetInheritedBindingContext(image.Source, visualElement?.BindingContext);
}
public static async void ImageSourceChanging(ImageSource oldImageSource)
{
if (oldImageSource == null) return;
try
{
await oldImageSource.Cancel().ConfigureAwait(false);
}
catch (ObjectDisposedException)
{
// Workaround bugzilla 37792 https://bugzilla.xamarin.com/show_bug.cgi?id=37792
}
}
public static void ImageSourceChanged(BindableObject bindable, ImageSource newSource)
{
var visualElement = (VisualElement)bindable;
if (newSource != null)
BindableObject.SetInheritedBindingContext(newSource, visualElement?.BindingContext);
visualElement?.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}
public static void ImageSourcesSourceChanged(object sender, EventArgs e)
{
((IImageController)sender).RaiseImageSourcePropertyChanged();
((VisualElement)sender).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}
}
}

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

@ -19,6 +19,8 @@ namespace Xamarin.Forms.Platform.Android
bool _drawableEnabled;
bool _disposed;
IBorderVisualElementRenderer _renderer;
private IBorderElement _borderElement;
VisualElement Element => _renderer?.Element;
AView Control => _renderer?.View;
readonly bool _drawOutlineWithBackground;
@ -41,38 +43,47 @@ namespace Xamarin.Forms.Platform.Android
{
if (e.OldElement != null)
{
(e.OldElement as IBorderController).PropertyChanged -= BorderElementPropertyChanged;
if (e.OldElement is INotifyPropertyChanged oldElement)
oldElement.PropertyChanged -= BorderElementPropertyChanged;
}
if (e.NewElement != null)
{
if (BorderElement != null)
if (BorderPropertyChanged != null)
{
BorderElement.PropertyChanged -= BorderElementPropertyChanged;
BorderPropertyChanged.PropertyChanged -= BorderElementPropertyChanged;
}
BorderElement = (IBorderController)e.NewElement;
BorderElement.PropertyChanged += BorderElementPropertyChanged;
BorderElement = (IBorderElement)e.NewElement;
if (BorderPropertyChanged != null)
BorderPropertyChanged.PropertyChanged += BorderElementPropertyChanged;
}
Reset();
UpdateDrawable();
}
public IBorderController BorderElement
public IBorderElement BorderElement
{
get;
private set;
get => _borderElement;
private set
{
_borderElement = value;
BorderPropertyChanged = value as INotifyPropertyChanged;
}
}
INotifyPropertyChanged BorderPropertyChanged { get; set; }
public void UpdateDrawable()
{
if (BorderElement == null || Control == null)
return;
bool cornerRadiusIsDefault = !BorderElement.IsSet(BorderElement.CornerRadiusProperty) || (BorderElement.CornerRadius == (int)BorderElement.CornerRadiusProperty.DefaultValue || BorderElement.CornerRadius == BorderDrawable.DefaultCornerRadius);
bool backgroundColorIsDefault = !BorderElement.IsSet(VisualElement.BackgroundColorProperty) || BorderElement.BackgroundColor == (Color)VisualElement.BackgroundColorProperty.DefaultValue;
bool borderColorIsDefault = !BorderElement.IsSet(BorderElement.BorderColorProperty) || BorderElement.BorderColor == (Color)BorderElement.BorderColorProperty.DefaultValue;
bool borderWidthIsDefault = !BorderElement.IsSet(BorderElement.BorderWidthProperty) || BorderElement.BorderWidth == (double)BorderElement.BorderWidthProperty.DefaultValue;
bool cornerRadiusIsDefault = !BorderElement.IsCornerRadiusSet() || (BorderElement.CornerRadius == (int)BorderElement.CornerRadiusDefaultValue || BorderElement.CornerRadius == BorderDrawable.DefaultCornerRadius);
bool backgroundColorIsDefault = !BorderElement.IsBackgroundColorSet() || BorderElement.BackgroundColor == (Color)VisualElement.BackgroundColorProperty.DefaultValue;
bool borderColorIsDefault = !BorderElement.IsBorderColorSet() || BorderElement.BorderColor == (Color)BorderElement.BorderColorDefaultValue;
bool borderWidthIsDefault = !BorderElement.IsBorderWidthSet() || BorderElement.BorderWidth == (double)BorderElement.BorderWidthDefaultValue;
if (backgroundColorIsDefault
&& cornerRadiusIsDefault
@ -93,7 +104,7 @@ namespace Xamarin.Forms.Platform.Android
if (_backgroundDrawable == null)
_backgroundDrawable = new BorderDrawable(Control.Context.ToPixels, Forms.GetColorButtonNormal(Control.Context), _drawOutlineWithBackground);
_backgroundDrawable.BorderController = BorderElement;
_backgroundDrawable.BorderElement = BorderElement;
var useDefaultPadding = _renderer.UseDefaultPadding();
@ -191,12 +202,13 @@ namespace Xamarin.Forms.Platform.Android
_defaultDrawable = null;
_rippleDrawable?.Dispose();
_rippleDrawable = null;
if (BorderElement != null)
if (BorderPropertyChanged != null)
{
BorderElement.PropertyChanged -= BorderElementPropertyChanged;
BorderElement = null;
BorderPropertyChanged.PropertyChanged -= BorderElementPropertyChanged;
}
BorderElement = null;
if (_renderer != null)
{
_renderer.ElementChanged -= OnElementChanged;
@ -209,9 +221,9 @@ namespace Xamarin.Forms.Platform.Android
void BorderElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals(BorderElement.BorderColorProperty.PropertyName) ||
e.PropertyName.Equals(BorderElement.BorderWidthProperty.PropertyName) ||
e.PropertyName.Equals(BorderElement.CornerRadiusProperty.PropertyName) ||
if (e.PropertyName.Equals(Button.BorderColorProperty.PropertyName) ||
e.PropertyName.Equals(Button.BorderWidthProperty.PropertyName) ||
e.PropertyName.Equals(Button.CornerRadiusProperty.PropertyName) ||
e.PropertyName.Equals(VisualElement.BackgroundColorProperty.PropertyName) ||
e.PropertyName.Equals(Specifics.Button.UseDefaultPaddingProperty.PropertyName) ||
e.PropertyName.Equals(Specifics.Button.UseDefaultShadowProperty.PropertyName) ||

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

@ -8,7 +8,7 @@ namespace Xamarin.Forms.Platform.Android
{
internal static class ImageViewExtensions
{
public static Task UpdateBitmap(this AImageView imageView, IImageController newView, IImageController previousView) =>
public static Task UpdateBitmap(this AImageView imageView, IImageElement newView, IImageElement previousView) =>
imageView.UpdateBitmap(newView, previousView, null, null);
public static Task UpdateBitmap(this AImageView imageView, ImageSource newImageSource, ImageSource previousImageSourc) =>
@ -17,12 +17,13 @@ namespace Xamarin.Forms.Platform.Android
// TODO hartez 2017/04/07 09:33:03 Review this again, not sure it's handling the transition from previousImage to 'null' newImage correctly
static async Task UpdateBitmap(
this AImageView imageView,
IImageController newView,
IImageController previousView,
IImageElement newView,
IImageElement previousView,
ImageSource newImageSource,
ImageSource previousImageSource)
{
IImageController imageController = newView as IImageController;
newImageSource = newView?.Source;
previousImageSource = previousView?.Source;
@ -32,7 +33,7 @@ namespace Xamarin.Forms.Platform.Android
if (Equals(previousImageSource, newImageSource))
return;
newView?.SetIsLoading(true);
imageController?.SetIsLoading(true);
(imageView as IImageRendererController)?.SkipInvalidate();
imageView.SetImageResource(global::Android.Resource.Color.Transparent);
@ -59,7 +60,7 @@ namespace Xamarin.Forms.Platform.Android
}
catch (TaskCanceledException)
{
newView?.SetIsLoading(false);
imageController?.SetIsLoading(false);
}
// Check if the source on the new image has changed since the image was loaded
@ -78,7 +79,7 @@ namespace Xamarin.Forms.Platform.Android
}
bitmap?.Dispose();
newView?.SetIsLoading(false);
imageController?.SetIsLoading(false);
}
}
}

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

@ -34,8 +34,8 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
{
var renderer = (sender as IVisualElementRenderer);
var view = renderer.View as ImageView;
var newImageElementManager = e.NewElement as IImageController;
var oldImageElementManager = e.OldElement as IImageController;
var newImageElementManager = e.NewElement as IImageElement;
var oldImageElementManager = e.OldElement as IImageElement;
var rendererController = renderer as IImageRendererController;
await TryUpdateBitmap(rendererController, view, newImageElementManager, oldImageElementManager);
@ -50,12 +50,15 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
async static void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var renderer = (sender as IVisualElementRenderer);
var ImageElementManager = (IImageController)renderer.Element;
if (e.PropertyName == ImageElementManager.SourceProperty?.PropertyName)
var ImageElementManager = (IImageElement)renderer.Element;
var imageController = (IImageController)renderer.Element;
if (e.PropertyName == Image.SourceProperty.PropertyName ||
e.PropertyName == Button.ImageProperty.PropertyName)
{
try
{
await TryUpdateBitmap(renderer as IImageRendererController, (ImageView)renderer.View, (IImageController)renderer.Element).ConfigureAwait(false);
await TryUpdateBitmap(renderer as IImageRendererController, (ImageView)renderer.View, (IImageElement)renderer.Element).ConfigureAwait(false);
}
catch (Exception ex)
{
@ -63,17 +66,18 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
}
finally
{
ImageElementManager?.SetIsLoading(false);
if(imageController != null)
imageController?.SetIsLoading(false);
}
}
else if (e.PropertyName == ImageElementManager.AspectProperty?.PropertyName)
else if (e.PropertyName == Image.AspectProperty.PropertyName)
{
UpdateAspect(renderer as IImageRendererController, (ImageView)renderer.View, (IImageController)renderer.Element);
UpdateAspect(renderer as IImageRendererController, (ImageView)renderer.View, (IImageElement)renderer.Element);
}
}
async static Task TryUpdateBitmap(IImageRendererController rendererController, ImageView Control, IImageController newImage, IImageController previous = null)
async static Task TryUpdateBitmap(IImageRendererController rendererController, ImageView Control, IImageElement newImage, IImageElement previous = null)
{
if (newImage == null || rendererController.IsDisposed)
{
@ -83,7 +87,7 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
await Control.UpdateBitmap(newImage, previous).ConfigureAwait(false);
}
static void UpdateAspect(IImageRendererController rendererController, ImageView Control, IImageController newImage, IImageController previous = null)
static void UpdateAspect(IImageRendererController rendererController, ImageView Control, IImageElement newImage, IImageElement previous = null)
{
if (newImage == null || rendererController.IsDisposed)
{

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

@ -44,7 +44,7 @@ namespace Xamarin.Forms.Platform.Android
_drawOutlineWithBackground = drawOutlineWithBackground;
}
public IBorderController BorderController
public IBorderElement BorderElement
{
get;
set;
@ -76,7 +76,7 @@ namespace Xamarin.Forms.Platform.Android
_normalBitmap.Width != width)
Reset();
if (!_drawOutlineWithBackground && BorderController.BackgroundColor == Color.Default)
if (!_drawOutlineWithBackground && BorderElement.BackgroundColor == Color.Default)
return;
Bitmap bitmap = null;
@ -141,7 +141,7 @@ namespace Xamarin.Forms.Platform.Android
{
}
public Color BackgroundColor => BorderController.BackgroundColor == Color.Default ? _defaultColor : BorderController.BackgroundColor;
public Color BackgroundColor => BorderElement.BackgroundColor == Color.Default ? _defaultColor : BorderElement.BackgroundColor;
public Color PressedBackgroundColor => BackgroundColor.AddLuminosity(-.12);//<item name="highlight_alpha_material_light" format="float" type="dimen">0.12</item>
protected override void Dispose(bool disposing)
@ -206,8 +206,8 @@ namespace Xamarin.Forms.Platform.Android
{
int cornerRadius = DefaultCornerRadius;
if (BorderController.IsSet(BorderController.CornerRadiusProperty) && BorderController.CornerRadius != (int)BorderController.CornerRadiusProperty.DefaultValue)
cornerRadius = BorderController.CornerRadius;
if (BorderElement.IsCornerRadiusSet() && BorderElement.CornerRadius != (int)BorderElement.CornerRadiusDefaultValue)
cornerRadius = BorderElement.CornerRadius;
return _convertToPixels(cornerRadius);
}
@ -221,13 +221,13 @@ namespace Xamarin.Forms.Platform.Android
public void DrawOutline(Canvas canvas, int width, int height)
{
if (BorderController.BorderWidth <= 0)
if (BorderElement.BorderWidth <= 0)
return;
using (var paint = new Paint { AntiAlias = true })
using (var path = new Path())
{
float borderWidth = _convertToPixels(BorderController.BorderWidth);
float borderWidth = _convertToPixels(BorderElement.BorderWidth);
float inset = borderWidth / 2;
// adjust border radius so outer edge of stroke is same radius as border radius of background
@ -239,7 +239,7 @@ namespace Xamarin.Forms.Platform.Android
path.AddRoundRect(rect, borderRadius, borderRadius, Path.Direction.Cw);
paint.StrokeWidth = borderWidth;
paint.SetStyle(Paint.Style.Stroke);
paint.Color = BorderController.BorderColor.ToAndroid();
paint.Color = BorderElement.BorderColor.ToAndroid();
canvas.DrawPath(path, paint);
}

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

@ -30,9 +30,9 @@ namespace Xamarin.Forms.Platform.UWP
static void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
IImageVisualElementRenderer renderer = sender as IImageVisualElementRenderer;
var controller = renderer.Element as IImageController;
var controller = renderer.Element as IImageElement;
if (e.PropertyName == controller.AspectProperty?.PropertyName)
if (e.PropertyName == Image.AspectProperty.PropertyName)
UpdateAspect(renderer, controller);
}
@ -41,7 +41,7 @@ namespace Xamarin.Forms.Platform.UWP
if (e.NewElement != null)
{
IImageVisualElementRenderer renderer = sender as IImageVisualElementRenderer;
var controller = renderer.Element as IImageController;
var controller = renderer.Element as IImageElement;
UpdateAspect(renderer, controller);
}
@ -53,12 +53,12 @@ namespace Xamarin.Forms.Platform.UWP
{
IImageVisualElementRenderer renderer = sender as IImageVisualElementRenderer;
var controller = renderer.Element as IImageController;
var controller = renderer.Element as IImageElement;
UpdateAspect(renderer, controller);
}
public static void UpdateAspect(IImageVisualElementRenderer renderer, IImageController controller)
public static void UpdateAspect(IImageVisualElementRenderer renderer, IImageElement controller)
{
var Element = renderer.Element;
var Control = renderer.GetNativeElement();
@ -101,16 +101,18 @@ namespace Xamarin.Forms.Platform.UWP
{
var Element = renderer.Element;
var Control = renderer.GetNativeElement();
var controller = Element as IImageController;
var imageElement = Element as IImageElement;
if (renderer.IsDisposed || Element == null || Control == null)
{
return;
}
controller.SetIsLoading(true);
var imageController = Element as IImageController;
ImageSource source = controller.Source;
imageController?.SetIsLoading(true);
ImageSource source = imageElement.Source;
IImageSourceHandler handler;
if (source != null && (handler = Registrar.Registered.GetHandlerForObject<IImageSourceHandler>(source)) != null)
{
@ -132,12 +134,12 @@ namespace Xamarin.Forms.Platform.UWP
renderer.SetImage(imagesource);
}
RefreshImage(controller as IViewController);
RefreshImage(imageElement as IViewController);
}
else
{
renderer.SetImage(null);
controller.SetIsLoading(false);
imageController?.SetIsLoading(false);
}
}

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

@ -25,9 +25,9 @@ namespace Xamarin.Forms.Platform.iOS
static void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
IVisualNativeElementRenderer renderer = (IVisualNativeElementRenderer)sender;
IBorderController backgroundView = (IBorderController)renderer.Element;
IBorderElement backgroundView = (IBorderElement)renderer.Element;
if (e.PropertyName == backgroundView.BorderWidthProperty.PropertyName || e.PropertyName == backgroundView.CornerRadiusProperty.PropertyName || e.PropertyName == backgroundView.BorderColorProperty.PropertyName)
if (e.PropertyName == Button.BorderWidthProperty.PropertyName || e.PropertyName == Button.CornerRadiusProperty.PropertyName || e.PropertyName == Button.BorderColorProperty.PropertyName)
UpdateBorder(renderer, backgroundView);
}
@ -35,11 +35,11 @@ namespace Xamarin.Forms.Platform.iOS
{
if (e.NewElement != null)
{
UpdateBorder((IVisualNativeElementRenderer)sender, (IBorderController)e.NewElement);
UpdateBorder((IVisualNativeElementRenderer)sender, (IBorderElement)e.NewElement);
}
}
public static void UpdateBorder(IVisualNativeElementRenderer renderer, IBorderController backgroundView)
public static void UpdateBorder(IVisualNativeElementRenderer renderer, IBorderElement backgroundView)
{
var control = renderer.Control;
var ImageButton = backgroundView;
@ -56,7 +56,7 @@ namespace Xamarin.Forms.Platform.iOS
nfloat cornerRadius = _defaultCornerRadius;
if (ImageButton.IsSet(ImageButton.CornerRadiusProperty) && ImageButton.CornerRadius != (int)ImageButton.CornerRadiusProperty.DefaultValue)
if (ImageButton.IsCornerRadiusSet() && ImageButton.CornerRadius != ImageButton.CornerRadiusDefaultValue)
cornerRadius = ImageButton.CornerRadius;
control.Layer.CornerRadius = cornerRadius;
@ -65,7 +65,7 @@ namespace Xamarin.Forms.Platform.iOS
static void OnControlChanged(object sender, EventArgs e)
{
IVisualNativeElementRenderer renderer = (IVisualNativeElementRenderer)sender;
IBorderController backgroundView = (IBorderController)renderer.Element;
IBorderElement backgroundView = (IBorderElement)renderer.Element;
UpdateBorder(renderer, backgroundView);
}
}

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

@ -29,7 +29,7 @@ namespace Xamarin.Forms.Platform.iOS
static void OnControlChanged(object sender, EventArgs e)
{
var renderer = sender as IImageVisualElementRenderer;
var imageElement = renderer.Element as IImageController;
var imageElement = renderer.Element as IImageElement;
SetAspect(renderer, imageElement);
SetOpacity(renderer, imageElement);
}
@ -39,7 +39,7 @@ namespace Xamarin.Forms.Platform.iOS
if (e.NewElement != null)
{
var renderer = sender as IImageVisualElementRenderer;
var imageElement = renderer.Element as IImageController;
var imageElement = renderer.Element as IImageElement;
SetAspect(renderer, imageElement);
SetOpacity(renderer, imageElement);
@ -49,17 +49,17 @@ namespace Xamarin.Forms.Platform.iOS
static void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var renderer = sender as IImageVisualElementRenderer;
var imageElement = renderer.Element as IImageController;
var imageElement = renderer.Element as IImageElement;
if (e.PropertyName == imageElement.IsOpaqueProperty?.PropertyName)
SetOpacity(renderer, renderer.Element as IImageController);
else if (e.PropertyName == imageElement.AspectProperty?.PropertyName)
SetAspect(renderer, renderer.Element as IImageController);
if (e.PropertyName == Image.IsOpaqueProperty.PropertyName)
SetOpacity(renderer, renderer.Element as IImageElement);
else if (e.PropertyName == Image.AspectProperty.PropertyName)
SetAspect(renderer, renderer.Element as IImageElement);
}
public static void SetAspect(IImageVisualElementRenderer renderer, IImageController imageElement)
public static void SetAspect(IImageVisualElementRenderer renderer, IImageElement imageElement)
{
var Element = renderer.Element;
var Control = renderer.GetImage();
@ -73,7 +73,7 @@ namespace Xamarin.Forms.Platform.iOS
Control.ContentMode = imageElement.Aspect.ToUIViewContentMode();
}
public static void SetOpacity(IImageVisualElementRenderer renderer, IImageController imageElement)
public static void SetOpacity(IImageVisualElementRenderer renderer, IImageElement imageElement)
{
var Element = renderer.Element;
var Control = renderer.GetImage();
@ -86,7 +86,7 @@ namespace Xamarin.Forms.Platform.iOS
Control.Opaque = imageElement.IsOpaque;
}
public static async Task SetImage(IImageVisualElementRenderer renderer, IImageController imageElement, Image oldElement = null)
public static async Task SetImage(IImageVisualElementRenderer renderer, IImageElement imageElement, Image oldElement = null)
{
_ = renderer ?? throw new ArgumentNullException($"{nameof(ImageElementManager)}.{nameof(SetImage)} {nameof(renderer)} cannot be null");
_ = imageElement ?? throw new ArgumentNullException($"{nameof(ImageElementManager)}.{nameof(SetImage)} {nameof(imageElement)} cannot be null");
@ -99,6 +99,8 @@ namespace Xamarin.Forms.Platform.iOS
return;
}
var imageController = imageElement as IImageController;
var source = imageElement.Source;
if (oldElement != null)
@ -114,7 +116,7 @@ namespace Xamarin.Forms.Platform.iOS
}
IImageSourceHandler handler;
imageElement.SetIsLoading(true);
imageController?.SetIsLoading(true);
try
{
if (source != null &&
@ -147,7 +149,7 @@ namespace Xamarin.Forms.Platform.iOS
}
finally
{
imageElement.SetIsLoading(false);
imageController?.SetIsLoading(false);
}
(imageElement as IViewController)?.NativeSizeChanged();