Add ProgressBarRenderer

This commit is contained in:
Frank A. Krueger 2017-11-26 09:43:52 -08:00
parent 89652d8779
commit b7d7bbfbb5
4 changed files with 55 additions and 0 deletions

View File

@ -10,6 +10,7 @@ using Xamarin.Forms.Internals;
[assembly: ExportRenderer (typeof (Button), typeof (ButtonRenderer))]
[assembly: ExportRenderer (typeof (Entry), typeof (EntryRenderer))]
[assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))]
[assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))]
namespace Ooui.Forms
{

View File

@ -0,0 +1,51 @@
using System;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms;
namespace Ooui.Forms.Renderers
{
public class ProgressBarRenderer : ViewRenderer<ProgressBar, Div>
{
public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint)
{
var size = new Size (80, 20);
return new SizeRequest (size, size);
}
protected override void OnElementChanged (ElementChangedEventArgs<ProgressBar> e)
{
if (e.NewElement != null) {
if (Control == null) {
var p = new Div { ClassName = "progress" };
var pb = new Div { ClassName = "progress-bar progress-bar" };
pb.SetAttribute ("role", "progressbar");
pb.SetAttribute ("aria-valuenow", "0");
pb.SetAttribute ("aria-valuemin", "0");
pb.SetAttribute ("aria-valuemax", "100");
pb.Style.Width = "0%";
p.AppendChild (pb);
SetNativeControl (p);
}
UpdateProgress ();
}
base.OnElementChanged (e);
}
protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged (sender, e);
if (e.PropertyName == ProgressBar.ProgressProperty.PropertyName)
UpdateProgress ();
}
void UpdateProgress ()
{
var pb = Control?.Children.FirstOrDefault () as Div;
pb.Style.Width = string.Format (System.Globalization.CultureInfo.InvariantCulture, "{0}%", Element.Progress*100);
}
}
}

View File

@ -6,6 +6,7 @@
<StackLayout>
<Label Text="Welcome to DisplayAlert Sample!" />
<ActivityIndicator x:Name="activity" />
<ProgressBar x:Name="progress" />
<Button Text="Tap for Display Alert"
Clicked="OnButtonClicked" />
</StackLayout>

View File

@ -17,9 +17,11 @@ namespace Samples
public async void OnButtonClicked(object sender, EventArgs args)
{
activity.IsRunning = true;
progress.Progress = 0.5;
var result = await DisplayAlert("Alert Message", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa.", "YES", "NO");
await DisplayAlert("Alert Response", $"You selected value: {result}", "OK");
activity.IsRunning = false;
progress.Progress = 1.0;
}
}
}