-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from ericsink/master
WIP: Xamarin.Forms Picker
- Loading branch information
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters