Add FrameRenderer

This commit is contained in:
Frank A. Krueger 2017-12-09 22:37:25 -08:00
parent 33e1a65f59
commit 4f047d1e85
3 changed files with 76 additions and 9 deletions

View File

@ -11,6 +11,7 @@ using Xamarin.Forms.Internals;
[assembly: ExportRenderer (typeof (DatePicker), typeof (DatePickerRenderer))]
[assembly: ExportRenderer (typeof (Editor), typeof (EditorRenderer))]
[assembly: ExportRenderer (typeof (Entry), typeof (EntryRenderer))]
[assembly: ExportRenderer (typeof (Frame), typeof (FrameRenderer))]
[assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))]
[assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))]

View File

@ -0,0 +1,64 @@
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Ooui.Forms.Extensions;
namespace Ooui.Forms.Renderers
{
public class FrameRenderer : VisualElementRenderer<Frame>
{
protected override void OnElementChanged (ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged (e);
if (e.NewElement != null)
SetupLayer ();
}
protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged (sender, e);
if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName ||
e.PropertyName == Xamarin.Forms.Frame.OutlineColorProperty.PropertyName ||
e.PropertyName == Xamarin.Forms.Frame.HasShadowProperty.PropertyName ||
e.PropertyName == Xamarin.Forms.Frame.CornerRadiusProperty.PropertyName)
SetupLayer ();
}
void SetupLayer ()
{
float cornerRadius = Element.CornerRadius;
if (cornerRadius == -1f)
cornerRadius = 5f; // default corner radius
var Layer = this.Style;
Layer.BorderRadius = cornerRadius;
if (Element.BackgroundColor == Xamarin.Forms.Color.Default)
Layer.BackgroundColor = "white";
else
Layer.BackgroundColor = Element.BackgroundColor.ToOouiColor ();
if (Element.HasShadow) {
//Layer.ShadowRadius = 5;
//Layer.ShadowColor = "black";
//Layer.ShadowOpacity = 0.8f;
//Layer.ShadowOffset = new SizeF ();
}
else {
//Layer.ShadowOpacity = 0;
}
if (Element.OutlineColor == Xamarin.Forms.Color.Default)
Layer.BorderColor = Colors.Clear;
else {
Layer.BorderColor = Element.OutlineColor.ToOouiColor ();
Layer.BorderWidth = 1;
Layer.BorderStyle = "solid";
}
}
}
}

View File

@ -93,38 +93,38 @@ namespace Ooui
public Value BorderTopWidth {
get => this["border-top-width"];
set => this["border-top-width"] = value;
set => this["border-top-width"] = AddNumberUnits (value, "px");
}
public Value BorderRightWidth {
get => this["border-right-width"];
set => this["border-right-width"] = value;
set => this["border-right-width"] = AddNumberUnits (value, "px");
}
public Value BorderBottomWidth {
get => this["border-bottom-width"];
set => this["border-bottom-width"] = value;
set => this["border-bottom-width"] = AddNumberUnits (value, "px");
}
public Value BorderLeftWidth {
get => this["border-left-width"];
set => this["border-left-width"] = value;
set => this["border-left-width"] = AddNumberUnits (value, "px");
}
public Value BorderRadius {
get => this["border-radius"];
set {
this["border-radius"] = value;
this["border-radius"] = AddNumberUnits (value, "px");
}
}
public Value BorderWidth {
get => this["border-top-width"];
set {
this["border-top-width"] = value;
this["border-right-width"] = value;
this["border-bottom-width"] = value;
this["border-left-width"] = value;
this["border-top-width"] = AddNumberUnits (value, "px");
this["border-right-width"] = AddNumberUnits (value, "px");
this["border-bottom-width"] = AddNumberUnits (value, "px");
this["border-left-width"] = AddNumberUnits (value, "px");
}
}
@ -403,6 +403,8 @@ namespace Ooui
static string AddNumberUnits (object val, string units)
{
if (val == null)
return null;
if (val is string s)
return s;
if (val is IConvertible c)