Skip to content

Instantly share code, notes, and snippets.

@detroitpro
Created June 6, 2014 04:46
Show Gist options
  • Save detroitpro/f94fd07ee4a907d1575c to your computer and use it in GitHub Desktop.
Save detroitpro/f94fd07ee4a907d1575c to your computer and use it in GitHub Desktop.
Segmented Render for Xamarin.Forms.
public class SegmentedControlRenderer : NativeRenderer
{
protected override void OnModelSet(VisualElement model)
{
base.OnModelSet (model);
SegmentedControlView segmentedControlView = (SegmentedControlView)this.Model;
var native = new UISegmentedControl ();
native.Frame = new RectangleF (20, 20, 280, 40);
// Create the view using the properties from the model.
native.InsertSegment ("One", 0, false);
native.InsertSegment ("Two", 1, false);
native.SelectedSegment = segmentedControlView.SelectedItem;
native.ValueChanged += (s, e) => {
var ctrl = s as UISegmentedControl;
segmentedControlView.SelectedItem = ctrl.SelectedSegment;
};
// Add it as a child to this view by setting it as the control
// for this renderer.
SetNativeControl (native);
}
}
public class SegmentedControlView : BoxView
{
public SegmentedControlView()
{
}
public static readonly BindableProperty SelectedItemProperty =
BindableProperty.Create<SegmentedControlView,int> (
p => p.SelectedItem, default(int));
public int SelectedItem {
get { return (int)GetValue (SelectedItemProperty); }
set {
Debug.WriteLine ("New Value:" + value);
SetValue (SelectedItemProperty, value);
}
}
}
@sunil1343
Copy link

I'm getting lot of errors while this code in my project will you please tell the libraries to be used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment