Skip to content

Instantly share code, notes, and snippets.

@tombowers
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tombowers/9119f7f240170a769d14 to your computer and use it in GitHub Desktop.
Save tombowers/9119f7f240170a769d14 to your computer and use it in GitHub Desktop.
public class CustomAndroidEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.OldElement == null) {
// Get the 'native' control
var nativeTextField = (EditText)Control;
nativeTextField.Text = "Ahoy there from Android!";
}
}
}
new CustomEntry
{
Text = "Space Pants"
}
public class CustomEntry : Entry {}
public class CustomIOSEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.OldElement == null) {
// Get the 'native' control
var nativeTextField = (UITextField)Control;
nativeTextField.Text = "Ahoy there from iOS!";
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CustomEntryRenderer;assembly=YourAssembly"
x:Class="CustomEntryRenderer.MainPageXaml">
<StackLayout
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand">
<Label Text="Hello, Custom Renderer!" />
<local:CustomEntry Text="In Shared Xaml" />
</StackLayout>
</ContentPage>
public class MainPage : ContentPage
{
public MainPage()
{
Content = new StackLayout
{
Children =
{
new Entry
{
Text = "Ahoy there!"
},
new Button
{
Text = "Login",
TextColor = Color.White,
BackgroundColor = Color.FromHex("77D065")
}
},
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand
};
}
}
[assembly: ExportRenderer(typeof (CustomEntry), typeof(CustomEntryRenderer))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment