Add Frame component to enable rounded corners

This commit is contained in:
Eilon Lipton 2019-10-18 15:17:20 -07:00
Родитель e3f3936470
Коммит 20088b6cee
8 изменённых файлов: 186 добавлений и 32 удалений

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

@ -1,39 +1,43 @@
@inject AppState AppState
<StackLayout>
<StackLayout Orientation="StackOrientation.Horizontal">
@* Input area *@
<Entry @bind-Text="newText"
Placeholder="What's to be done?"
OnCompleted="@AddItem"
IsEnabled="@(!isAdding)"
HorizontalOptions="LayoutOptions.FillAndExpand" />
<Button Text="Add"
OnClick="@AddItem"
IsEnabled="@(!isAdding && !string.IsNullOrWhiteSpace(newText))" />
<ActivityIndicator Color="Color.Orange"
IsRunning="@isAdding" />
</StackLayout>
<ScrollView>
<StackLayout>
@* List of todos *@
@if (items == null)
{
<StackLayout Orientation="StackOrientation.Horizontal">
<Label Text="Loading..." />
<ActivityIndicator Color="Color.Purple"
IsRunning="true" />
</StackLayout>
}
else
{
foreach (var item in items)
{
<TodoEntry Item="@item" />
}
}
<Frame BorderColor="@Color.LightBlue" CornerRadius="10" Margin="5" Padding="10">
<StackLayout Orientation="StackOrientation.Horizontal">
@* Input area *@
<Entry @bind-Text="newText"
Placeholder="What's to be done?"
OnCompleted="@AddItem"
IsEnabled="@(!isAdding)"
HorizontalOptions="LayoutOptions.FillAndExpand" />
<Button Text="Add"
OnClick="@AddItem"
IsEnabled="@(!isAdding && !string.IsNullOrWhiteSpace(newText))" />
<ActivityIndicator Color="Color.Orange"
IsRunning="@isAdding" />
</StackLayout>
</ScrollView>
</Frame>
<Frame BorderColor="@Color.LightBlue" CornerRadius="10" Margin="5" Padding="10">
<ScrollView>
<StackLayout>
@* List of todos *@
@if (items == null)
{
<StackLayout Orientation="StackOrientation.Horizontal">
<Label Text="Loading..." />
<ActivityIndicator Color="Color.Purple"
IsRunning="true" />
</StackLayout>
}
else
{
foreach (var item in items)
{
<TodoEntry Item="@item" />
}
}
</StackLayout>
</ScrollView>
</Frame>
</StackLayout>
@code

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

@ -33,5 +33,21 @@ namespace Blaxamarin.Framework.Elements
{
return double.Parse(doubleString, CultureInfo.InvariantCulture);
}
/// <summary>
/// Helper method to serialize <see cref="System.Single" /> objects.
/// </summary>
public static string SingleToString(float color)
{
return color.ToString("R", CultureInfo.InvariantCulture); // "R" --> Round-trip format specifier guarantees fidelity when parsing
}
/// <summary>
/// Helper method to deserialize <see cref="System.Single" /> objects.
/// </summary>
public static float StringToSingle(string singleString)
{
return float.Parse(singleString, CultureInfo.InvariantCulture);
}
}
}

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

@ -0,0 +1,22 @@
using Blaxamarin.Framework.Elements.Handlers;
using Emblazon;
using Microsoft.AspNetCore.Components;
using XF = Xamarin.Forms;
namespace Blaxamarin.Framework.Elements
{
public class ContentView : TemplatedView
{
static ContentView()
{
ElementHandlerRegistry.RegisterElementHandler<ContentView>(
renderer => new ContentViewHandler(renderer, new XF.ContentView()));
}
#pragma warning disable CA1721 // Property names should not match get methods
[Parameter] public RenderFragment ChildContent { get; set; }
#pragma warning restore CA1721 // Property names should not match get methods
protected override RenderFragment GetChildContent() => ChildContent;
}
}

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

@ -0,0 +1,38 @@
using Blaxamarin.Framework.Elements.Handlers;
using Emblazon;
using Microsoft.AspNetCore.Components;
using XF = Xamarin.Forms;
namespace Blaxamarin.Framework.Elements
{
public class Frame : ContentView
{
static Frame()
{
ElementHandlerRegistry
.RegisterElementHandler<Frame>(renderer => new FrameHandler(renderer, new XF.Frame()));
}
[Parameter] public bool? HasShadow { get; set; }
[Parameter] public XF.Color? BorderColor { get; set; }
[Parameter] public float? CornerRadius { get; set; }
protected override void RenderAttributes(AttributesBuilder builder)
{
base.RenderAttributes(builder);
if (HasShadow != null)
{
builder.AddAttribute(nameof(HasShadow), HasShadow.Value);
}
if (BorderColor != null)
{
builder.AddAttribute(nameof(BorderColor), AttributeHelper.ColorToString(BorderColor.Value));
}
if (CornerRadius != null)
{
builder.AddAttribute(nameof(CornerRadius), AttributeHelper.SingleToString(CornerRadius.Value));
}
}
}
}

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

@ -0,0 +1,15 @@
using Emblazon;
using XF = Xamarin.Forms;
namespace Blaxamarin.Framework.Elements.Handlers
{
public class ContentViewHandler : TemplatedViewHandler
{
public ContentViewHandler(EmblazonRenderer renderer, XF.ContentView contentViewControl) : base(renderer, contentViewControl)
{
ContentViewControl = contentViewControl ?? throw new System.ArgumentNullException(nameof(contentViewControl));
}
public XF.ContentView ContentViewControl { get; }
}
}

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

@ -0,0 +1,35 @@
using Emblazon;
using System;
using XF = Xamarin.Forms;
namespace Blaxamarin.Framework.Elements.Handlers
{
public class FrameHandler : ContentViewHandler
{
public FrameHandler(EmblazonRenderer renderer, XF.Frame frameControl) : base(renderer, frameControl)
{
FrameControl = frameControl ?? throw new ArgumentNullException(nameof(frameControl));
}
public XF.Frame FrameControl { get; }
public override void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName)
{
switch (attributeName)
{
case nameof(XF.Frame.HasShadow):
FrameControl.HasShadow = AttributeHelper.GetBool(attributeValue);
break;
case nameof(XF.Frame.BorderColor):
FrameControl.BorderColor = AttributeHelper.StringToColor((string)attributeValue);
break;
case nameof(XF.Frame.CornerRadius):
FrameControl.CornerRadius = AttributeHelper.StringToSingle((string)attributeValue);
break;
default:
base.ApplyAttribute(attributeEventHandlerId, attributeName, attributeValue, attributeEventUpdatesAttributeName);
break;
}
}
}
}

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

@ -0,0 +1,16 @@
using Emblazon;
using System;
using XF = Xamarin.Forms;
namespace Blaxamarin.Framework.Elements.Handlers
{
public class TemplatedViewHandler : LayoutHandler
{
public TemplatedViewHandler(EmblazonRenderer renderer, XF.TemplatedView templatedViewControl) : base(renderer, templatedViewControl)
{
TemplatedViewControl = templatedViewControl ?? throw new ArgumentNullException(nameof(templatedViewControl));
}
public XF.TemplatedView TemplatedViewControl { get; }
}
}

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

@ -0,0 +1,8 @@
namespace Blaxamarin.Framework.Elements
{
public class TemplatedView : Layout
{
// TODO: Look in the future how to support a ControlTemplate:
// https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/control-templates/creating
}
}