Last active
October 9, 2017 10:51
-
-
Save mogwai/11b15b00703ae3bbac6884879ac8dab0 to your computer and use it in GitHub Desktop.
Linking with Custom Renderers
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
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); } | |
} | |
} | |
} |
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
[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