Merge pull request #94 from ericsink/master

WIP: Xamarin.Forms Picker
This commit is contained in:
Frank A. Krueger 2018-03-23 17:23:08 -07:00 committed by GitHub
commit 190f1027a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 165 additions and 0 deletions

View File

@ -16,6 +16,7 @@ using Xamarin.Forms;
[assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))]
[assembly: ExportRenderer (typeof (LinkLabel), typeof (LinkLabelRenderer))]
[assembly: ExportRenderer (typeof (LinkView), typeof (LinkViewRenderer))]
[assembly: ExportRenderer(typeof(Picker), typeof(PickerRenderer))]
[assembly: ExportRenderer(typeof(ListView), typeof(ListViewRenderer))]
[assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))]
[assembly: ExportRenderer (typeof (SearchBar), typeof (SearchBarRenderer))]

View File

@ -0,0 +1,88 @@
using Ooui.Forms.Extensions;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
namespace Ooui.Forms.Renderers
{
public class PickerRenderer : ViewRenderer<Picker, Select>
{
private bool _disposed;
private Select _select;
public PickerRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
if (e.NewElement != null)
{
if (Control == null)
{
_select = new Select();
_select.Change += _select_Change;
SetNativeControl(_select);
}
UpdateItems();
UpdateBackgroundColor();
}
base.OnElementChanged(e);
}
private void _select_Change(object sender, TargetEventArgs e)
{
Element.SetValueFromRenderer(Picker.SelectedIndexProperty, Element.ItemsSource.IndexOf(_select.Value));
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Picker.ItemsSourceProperty.PropertyName)
{
UpdateItems();
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing && !_disposed)
{
_select.Change -= _select_Change;
_disposed = true;
}
}
private void UpdateBackgroundColor()
{
var backgroundColor = Element.BackgroundColor.ToOouiColor();
_select.Style.BackgroundColor = backgroundColor;
}
private void UpdateItems()
{
var items = Element.ItemsSource;
if (items != null)
{
foreach (var item in items)
{
var s = item.ToString();
_select.AddOption(s, s);
}
}
}
}
}

75
Samples/PickerSample.cs Normal file
View File

@ -0,0 +1,75 @@
using Ooui;
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Samples
{
public class PickerSample : ISample
{
private Xamarin.Forms.Label _label;
private Xamarin.Forms.Picker _picker;
public string Title => "Xamarin.Forms Picker Sample";
List<string> myItems = new List<string>
{
"red",
"green",
"yellow",
"blue",
"white",
"black",
"purple",
"orange",
};
public Ooui.Element CreateElement()
{
var panel = new StackLayout();
var titleLabel = new Xamarin.Forms.Label
{
Text = "Picker",
FontSize = 24,
FontAttributes = FontAttributes.Bold
};
panel.Children.Add(titleLabel);
_picker = new Picker
{
Title = "Hello",
//VerticalOptions = LayoutOptions.CenterAndExpand,
ItemsSource = myItems,
};
panel.Children.Add(_picker);
_picker.SelectedIndexChanged += OnPickerValueChanged;
_label = new Xamarin.Forms.Label
{
Text = "Picker value is",
HorizontalOptions = LayoutOptions.Center
};
panel.Children.Add(_label);
var page = new ContentPage
{
Content = panel
};
return page.GetOouiElement();
}
void OnPickerValueChanged(object sender, EventArgs e)
{
_label.Text = String.Format("Picker value is {0} ({1})", _picker.SelectedIndex, myItems[_picker.SelectedIndex]);
}
public void Publish()
{
UI.Publish("/picker", CreateElement);
}
}
}

View File

@ -43,6 +43,7 @@ namespace Samples
new WeatherAppSample().Publish();
new XuzzleSample().Publish();
new WebViewSample().Publish();
new PickerSample().Publish();
UI.Present ("/display-alert");