static BaseView() { var ctors = typeof(TPainter).GetConstructors().Where(c => c.GetParameters().All(param => param.IsOptional)); if (ctors.IsEmpty()) { throw new ArgumentException($"The supplied generic type parameter for {nameof(TPainter)}, which is {typeof(TPainter)}, does not have any constructors with no parameters nor all optional parameters."); } painterCtor = ctors.First(); ctorParams = Enumerable.Repeat(Type.Missing, painterCtor.GetParameters().Length).ToArray(); var painter = (TPainter)painterCtor.Invoke(ctorParams); var thisType = typeof(BaseView <TPainter, TSource>); var drawMethodParams = typeof(TPainter).GetMethod(nameof(ICanvasPainter <SKCanvas, TSource, SKColor> .Draw), new[] { typeof(SKCanvas), typeof(TextAlignment), typeof(Thickness), typeof(float), typeof(float) }).GetParameters(); TPainter p(BindableObject b) => ((BaseView <TPainter, TSource>)b).Painter; SourceProperty = BindableProperty.Create(nameof(Source), typeof(TSource), thisType, painter.Source, BindingMode.TwoWay, null, (b, o, n) => { p(b).Source = (TSource)n; ((BaseView <TPainter, TSource>)b).ErrorMessage = p(b).ErrorMessage; }); DisplayErrorInlineProperty = BindableProperty.Create(nameof(DisplayErrorInline), typeof(bool), thisType, painter.DisplayErrorInline, propertyChanged: (b, o, n) => p(b).DisplayErrorInline = (bool)n); FontSizeProperty = BindableProperty.Create(nameof(FontSize), typeof(float), thisType, painter.FontSize, propertyChanged: (b, o, n) => p(b).FontSize = (float)n); ErrorFontSizeProperty = BindableProperty.Create(nameof(ErrorFontSize), typeof(float?), thisType, painter.ErrorFontSize, propertyChanged: (b, o, n) => p(b).ErrorFontSize = (float)n); TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), thisType, painter.TextColor.ToFormsColor(), propertyChanged: (b, o, n) => p(b).TextColor = ((Color)n).ToSKColor()); HighlightColorProperty = BindableProperty.Create(nameof(HighlightColor), typeof(Color), thisType, painter.HighlightColor.ToFormsColor(), propertyChanged: (b, o, n) => p(b).HighlightColor = ((Color)n).ToSKColor()); ErrorColorProperty = BindableProperty.Create(nameof(ErrorColor), typeof(Color), thisType, painter.ErrorColor.ToFormsColor(), propertyChanged: (b, o, n) => p(b).ErrorColor = ((Color)n).ToSKColor()); TextAlignmentProperty = BindableProperty.Create(nameof(TextAlignment), typeof(TextAlignment), thisType, drawMethodParams[1].DefaultValue is DBNull ? default(TextAlignment) : drawMethodParams[1].DefaultValue ?? default(TextAlignment)); DisplacementXProperty = BindableProperty.Create(nameof(DisplacementX), typeof(float), thisType, drawMethodParams[3].DefaultValue, BindingMode.TwoWay); DisplacementYProperty = BindableProperty.Create(nameof(DisplacementY), typeof(float), thisType, drawMethodParams[4].DefaultValue, BindingMode.TwoWay); MagnificationProperty = BindableProperty.Create(nameof(Magnification), typeof(float), thisType, painter.Magnification, propertyChanged: (b, o, n) => p(b).Magnification = (float)n); PaintStyleProperty = BindableProperty.Create(nameof(PaintStyle), typeof(PaintStyle), thisType, painter.PaintStyle, propertyChanged: (b, o, n) => p(b).PaintStyle = (PaintStyle)n); LineStyleProperty = BindableProperty.Create(nameof(LineStyle), typeof(Enumerations.LineStyle), thisType, painter.LineStyle, propertyChanged: (b, o, n) => p(b).LineStyle = (Enumerations.LineStyle)n); PaddingProperty = BindableProperty.Create(nameof(Padding), typeof(Thickness), thisType, drawMethodParams[2].DefaultValue ?? default(Thickness)); ErrorMessagePropertyKey = BindableProperty.CreateReadOnly(nameof(ErrorMessage), typeof(string), thisType, painter.ErrorMessage, BindingMode.OneWayToSource); ErrorMessageProperty = ErrorMessagePropertyKey.BindableProperty; }
public static BindableProperty CreateReadOnlyBindableProperty <T, U> ( string localPropName, U defaultVal = default(U), BindingMode bindingMode = BindingMode.OneWay, Action <T, U, U> callbackAction = null ) where T : class { return(BindableProperty.CreateReadOnly ( localPropName, typeof(U), typeof(T), defaultVal, bindingMode, propertyChanged: (bindable, oldVal, newVal) => { if (callbackAction != null) { var bindableAsOverlayButton = bindable as T; if (bindableAsOverlayButton != null) { callbackAction(bindableAsOverlayButton, (U)oldVal, (U)newVal); } } }).BindableProperty); }
public void TestBindingTwoWayOnReadOnly() { var bindablePropertyKey = BindableProperty.CreateReadOnly <MockBindable, string> (w => w.Foo, "DefaultValue", BindingMode.OneWayToSource); var bindableProperty = bindablePropertyKey.BindableProperty; var bindable = new MockBindable(); var vm = new MockViewModel(); bindable.SetBinding(bindableProperty, new Binding("Text", BindingMode.TwoWay)); Assert.DoesNotThrow(() => bindable.BindingContext = vm); Assert.AreEqual("DefaultValue", bindable.GetValue(bindableProperty)); }
static AtomPasswordBox() { HasUpperCasePropertyKey = BindableProperty.CreateReadOnly("HasUpperCase", typeof(bool), typeof(AtomPasswordBox), false, BindingMode.OneWayToSource); HasUpperCaseProperty = HasUpperCasePropertyKey.BindableProperty; HasLowerCasePropertyKey = BindableProperty.CreateReadOnly("HasLowercase", typeof(bool), typeof(AtomPasswordBox), false, BindingMode.OneWayToSource); HasLowerCaseProperty = HasLowerCasePropertyKey.BindableProperty; HasNumberPropertyKey = BindableProperty.CreateReadOnly("HasNumber", typeof(bool), typeof(AtomPasswordBox), false); HasNumberProperty = HasNumberPropertyKey.BindableProperty; HasSymbolPropertyKey = BindableProperty.CreateReadOnly("HasSymbol", typeof(bool), typeof(AtomPasswordBox), false); HasSymbolProperty = HasSymbolPropertyKey.BindableProperty; }
public static BindablePropertyKey CreateReadonlyProperty <T>( T?defaultValue = default, BindingMode mode = BindingMode.TwoWay, BindableProperty.BindingPropertyChangedDelegate?propertyChanged = null, [CallerMemberName] string?propertyName = null) { propertyName = RemoveLastOccurrence(propertyName !, KnownPropertyPattern); return(BindableProperty.CreateReadOnly( propertyName, typeof(T), typeof(BindableObject), defaultValue, mode, propertyChanged: propertyChanged)); }
public void TestReadOnly() { var bindablePropertyKey = BindableProperty.CreateReadOnly <MockBindable, string> (w => w.Foo, "DefaultValue"); var bindableProperty = bindablePropertyKey.BindableProperty; var bindable = new MockBindable(); Assert.AreEqual("DefaultValue", bindable.GetValue(bindableProperty)); bindable.SetValue(bindablePropertyKey, "Bar"); Assert.AreEqual("Bar", bindable.GetValue(bindableProperty)); Assert.Throws <InvalidOperationException> (() => bindable.SetValue(bindableProperty, "Baz")); Assert.AreEqual("Bar", bindable.GetValue(bindableProperty)); Assert.Throws <InvalidOperationException> (() => bindable.ClearValue(bindableProperty)); bindable.ClearValue(bindablePropertyKey); Assert.AreEqual("DefaultValue", bindable.GetValue(bindableProperty)); }
static SignaturePadView() { StrokeColorProperty = BindableProperty.Create( nameof(StrokeColor), typeof(Color), typeof(SignaturePadView), ImageConstructionSettings.DefaultStrokeColor, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).SignaturePadCanvas.StrokeColor = (Color)newValue); StrokeWidthProperty = BindableProperty.Create( nameof(StrokeWidth), typeof(float), typeof(SignaturePadView), ImageConstructionSettings.DefaultStrokeWidth, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).SignaturePadCanvas.StrokeWidth = (float)newValue); SignatureLineColorProperty = BindableProperty.Create( nameof(SignatureLineColor), typeof(Color), typeof(SignaturePadView), SignaturePadDarkColor, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).SignatureLine.Color = (Color)newValue); SignatureLineWidthProperty = BindableProperty.Create( nameof(SignatureLineWidth), typeof(double), typeof(SignaturePadView), DefaultLineWidth, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).SignatureLine.HeightRequest = (double)newValue); SignatureLineSpacingProperty = BindableProperty.Create( nameof(SignatureLineSpacing), typeof(double), typeof(SignaturePadView), DefaultNarrowSpacing, propertyChanged: OnPaddingChanged); CaptionTextProperty = BindableProperty.Create( nameof(CaptionText), typeof(string), typeof(SignaturePadView), DefaultCaptionText, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).CaptionLabel.Text = (string)newValue); CaptionFontSizeProperty = BindableProperty.Create( nameof(CaptionFontSize), typeof(double), typeof(SignaturePadView), DefaultFontSize, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).CaptionLabel.FontSize = (double)newValue); CaptionTextColorProperty = BindableProperty.Create( nameof(CaptionTextColor), typeof(Color), typeof(SignaturePadView), SignaturePadDarkColor, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).CaptionLabel.TextColor = (Color)newValue); PromptTextProperty = BindableProperty.Create( nameof(PromptText), typeof(string), typeof(SignaturePadView), DefaultPromptText, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).SignaturePrompt.Text = (string)newValue); PromptFontSizeProperty = BindableProperty.Create( nameof(PromptFontSize), typeof(double), typeof(SignaturePadView), DefaultFontSize, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).SignaturePrompt.FontSize = (double)newValue); PromptTextColorProperty = BindableProperty.Create( nameof(PromptTextColor), typeof(Color), typeof(SignaturePadView), SignaturePadDarkColor, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).SignaturePrompt.TextColor = (Color)newValue); ClearTextProperty = BindableProperty.Create( nameof(ClearText), typeof(string), typeof(SignaturePadView), DefaultClearLabelText, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).ClearLabel.Text = (string)newValue); ClearFontSizeProperty = BindableProperty.Create( nameof(ClearFontSize), typeof(double), typeof(SignaturePadView), DefaultFontSize, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).ClearLabel.FontSize = (double)newValue); ClearTextColorProperty = BindableProperty.Create( nameof(ClearTextColor), typeof(Color), typeof(SignaturePadView), SignaturePadDarkColor, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).ClearLabel.TextColor = (Color)newValue); BackgroundImageProperty = BindableProperty.Create( nameof(BackgroundImage), typeof(ImageSource), typeof(SignaturePadView), default(ImageSource), propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).BackgroundImageView.Source = (ImageSource)newValue); BackgroundImageAspectProperty = BindableProperty.Create( nameof(BackgroundImageAspect), typeof(Aspect), typeof(SignaturePadView), Aspect.AspectFit, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).BackgroundImageView.Aspect = (Aspect)newValue); BackgroundImageOpacityProperty = BindableProperty.Create( nameof(BackgroundImageOpacity), typeof(double), typeof(SignaturePadView), 1.0, propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).BackgroundImageView.Opacity = (double)newValue); ClearedCommandProperty = BindableProperty.Create( nameof(ClearedCommand), typeof(ICommand), typeof(SignaturePadView), default(ICommand)); StrokeCompletedCommandProperty = BindableProperty.Create( nameof(StrokeCompletedCommand), typeof(ICommand), typeof(SignaturePadView), default(ICommand)); IsBlankPropertyKey = BindableProperty.CreateReadOnly( nameof(IsBlank), typeof(bool), typeof(SignaturePadView), true); IsBlankProperty = IsBlankPropertyKey.BindableProperty; }