Skip to content

Instantly share code, notes, and snippets.

@mogwai
Last active October 9, 2017 10:51
Show Gist options
  • Save mogwai/11b15b00703ae3bbac6884879ac8dab0 to your computer and use it in GitHub Desktop.
Save mogwai/11b15b00703ae3bbac6884879ac8dab0 to your computer and use it in GitHub Desktop.
Linking with Custom Renderers
using Xamarin.Forms;
namespace MyApp.Views.Controls {
public class CurvedSearchBar : SearchBar {
public static readonly BindableProperty CornerRadiusProperty =
BindableProperty.Create("CornerRadius", typeof(float), typeof(CurvedSearchBar), 0.0f);
public float CornerRadius {
get { return (float)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
}
}
[assembly: ExportRenderer(typeof(CurvedSearchBar), typeof(CurvedSearchBarRenderer))]
namespace MyApp.iOS.Renderer {
[Preserve(AllMembers = true)]
public class CurvedSearchBarRenderer : SearchBarRenderer {
// Required Default Constructor for Custom Renderers
public CurvedSearchBarRenderer() { }
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> args) {
base.OnElementChanged(args);
if (args.OldElement != null || args.NewElement == null)
return;
try {
Control.Layer.MasksToBounds = true;
var curvedsbar = (CurvedSearchBar)args.NewElement;
Control.Layer.CornerRadius = curvedsbar.CornerRadius;
} catch (Exception e) {
Debug.WriteLine(e);
}
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == CurvedSearchBar.CornerRadiusProperty.PropertyName) {
Control.Layer.CornerRadius = ((CurvedSearchBar)Element).CornerRadius;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment