- Article
- 6 minutes to read
Download the example
The Xamarin.Forms Entry control allows editing of a single line of text. This article shows how to create a custom renderer for the Entry control, allowing developers to override the default native rendering with their own platform-specific customization.
Every Xamarin.Forms control has a companion renderer for each platform that creates an instance of a native control. When aentrycontrol is rendered by a Xamarin.Forms application, in iOS theEntryRenders
Class is instantiated, which in turn instantiates a nativeUITextField
Control. On the Android platform is theEntryRenders
class instantiates aedit text
Control. On the Universal Windows Platform (UWP) is theEntryRenders
Class instantiates atext box
Control. For more information about the renderer and the native control classes that Xamarin.Forms controls map to, seeRenderer base classes and native controls.
The following diagram illustrates the relationship between theentrycontrol and the corresponding native controls that implement it:
The rendering process can be used to implement platform-specific customizations by creating a custom renderer for theentryControl on every platform. The process for this is as follows:
- Createa custom Xamarin.Forms control.
- Consumethe Xamarin.Forms custom control.
- Createthe custom renderer for the controls on each platform.
Each point will now be discussed in turn to implement oneentryControl that has a different background color on each platform.
Important
This article explains how to create a simple custom renderer. However, there is no need to create a custom renderer to implement oneentry
which has a different background color on each platform. This can be achieved more easily with the use ofDeviceclass or theonplatform
Markup extension to provide platform specific values. For more information, seeProviding platform-specific valuesAndOnPlatform markup extension.
Creating the custom input control
A traditionentryControl can be created by subclassingentry
control as shown in the following code example:
public class MyEntry : Entry{}
TheMeinEintrag
control is created in the .NET Standard library project and is simply aentryControl. Customization of the control is done in the custom renderer, so no additional implementation is required in theMeinEintrag
Control.
Consume the custom control
TheMeinEintrag
The control can be referenced in XAML in the .NET Standard library project by declaring a namespace for its location and using the namespace prefix for the control. The following code example shows how theMeinEintrag
The control can be used from a XAML page:
<ContentPage ... xmlns:local="clr-namespace:CustomRenderer;assembly=CustomRenderer" ...> ... <local:MyEntry Text="In Shared Code" /> ...</ContentPage>
Thelocal
Namespace prefix can be named anything. However, theclr-Namespace
AndMontage
The values must match the custom control details. Once the namespace is declared, the prefix is used to refer to the custom control.
The following code example shows how theMeinEintrag
The control can be used from a C# page:
public class MainPage : ContentPage{ public MainPage () { Content = new StackLayout { Children = { new Label { Text = "Hello, Custom Renderer !", }, new MyEntry { Text = "In Shared Code", } }, VerticalOptions = LayoutOptions.CenterAndExpand, HorizontalOptions = LayoutOptions.CenterAndExpand, }; }}
This code instantiates a new onecontent pageObject that displays alabelAndMeinEintrag
centered on the page both vertically and horizontally.
A custom renderer can now be added to any application project to customize the control's appearance on each platform.
Building the custom renderer on each platform
The process to create the custom renderer class is as follows:
- Create a subclass of the
EntryRenders
Class that renders the native control. - Overwrite the
OnElementChanged
method that renders the native control and writes logic to customize the control. This method is called when the corresponding Xamarin.Forms control is created. - Add one
ExportRenderer
-Attribute of the custom renderer class to indicate that it is used to render the Xamarin.Forms control. This attribute is used to register the custom renderer with Xamarin.Forms.
note
It is optional to deploy a custom renderer in each platform project. If no custom renderer is registered, the default renderer for the control's base class is used.
The following diagram illustrates the responsibilities of each project in the sample application and the relationships between them:
TheMeinEintrag
The control is platform-specificMyEntryRenderer
Classes that all derive from theEntryRenders
Class for every platform. This results in each caseMeinEintrag
Control is rendered with a platform-specific background color as shown in the screenshots below:
TheEntryRenders
class does itOnElementChanged
Method called when the Xamarin.Forms control is created to render the corresponding native control. This method takes aElementChangedEventArgs
Parameter that containsAltesElement
AndNewItem
Characteristics. These properties represent the Xamarin.Forms element that the rendererWarattached and the Xamarin.Forms element that the rendererIsattached or in the sample application theAltesElement
property will beNull
and theNewItem
property contains a reference to theMeinEintrag
Control.
An overwritten version of theOnElementChanged
method in theMyEntryRenderer
class is where the native control customization is done. A typed reference to the native control used on the platform can be accessed through thecontrol
Property. Additionally, a reference to the rendered Xamarin.Forms control can be obtained through theElement
property, although not used in the sample application.
Each custom renderer class is decorated with oneExportRenderer
attribute that registers the renderer with Xamarin.Forms. The attribute accepts two parameters - the type name of the rendered Xamarin.Forms control and the type name of the custom renderer. TheMontage
Attribute prefix indicates that the attribute applies to the entire assembly.
The following sections explain the implementation of each platform-specificMyEntryRenderer
custom renderer class.
Building the custom renderer on iOS
The following code example shows the custom renderer for the iOS platform:
using Xamarin.Forms.Platform.iOS;[assembly: ExportRenderer (typeof(MyEntry), typeof(MyEntryRenderer))]namespace CustomRenderer.iOS{ public class MyEntryRenderer : EntryRenderer { protected override void OnElementChanged (ElementChangedEventArgs<Entry> e) { base. OnElementChanged (e); if (Control != null) { // mache hier was du willst mit dem UITextField! Control.BackgroundColor = UIColor.FromRGB (204, 153, 255); Control.BorderStyle = UITextBorderStyle.Line; } } }}
The call to the base classOnElementChanged
method instantiates an iOSUITextField
control, assigning a reference to the control to that of the renderercontrol
Property. The background color is then set to light purple withUIColor.FromRGB
Method.
Building the custom renderer on Android
The following code example shows the custom renderer for the Android platform:
using Xamarin.Forms.Platform.Android;[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]namespace CustomRenderer.Android{ class MyEntryRenderer : EntryRenderer { public MyEntryRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged(e); if (Control != null) {Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen); } } }}
The call to the base classOnElementChanged
method instantiates an androidedit text
control, assigning a reference to the control to that of the renderercontrol
Property. The background color is then set to light greenControl.SetBackgroundColor
Method.
Building the custom renderer on UWP
The following code example shows the custom renderer for UWP:
[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]namespace CustomRenderer.UWP{ public class MyEntryRenderer : EntryRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged(e); if (Control != null) {Control.Background = new SolidColorBrush(Colors.Cyan); } } }}
The call to the base classOnElementChanged
Method instantiates atext box
control, assigning a reference to the control to that of the renderercontrol
Property. The background color is then set to cyan by creating aSolidColorBrush
Example.
Summary
This article showed how to create a custom control renderer for Xamarin.FormsentryControl allowing developers to override the default native rendering with their own platform-specific rendering. Custom renderers offer a powerful approach to customizing the appearance of Xamarin.Forms controls. They can be used for small style changes or sophisticated platform-specific layout and behavior tweaks.
- CustomRendererEntry (example)