Xamarin.Forms - Borderless Editor Using Custom Renderer

Before continuing with this article, I request you  read this previous article of mine - Xamarin.Forms - Borderless Entry Using Custom Renderer

 
 
Introduction
 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files are the most easily done tasks using native file APIs on each platform. Alternatively, embedded resources are also a simpler solution to distribute the data files with an app.

Custom Renderers


Xamarin.Forms user interfaces are rendered using the native controls of the target platform, allowing Xamarin.Forms applications to retain the appropriate look and feel for each platform. Custom Renderers let developers override this process to customize the appearance and behavior of Xamarin.Forms controls on each platform.
Prerequisites
  • Visual Studio 2017(Windows or Mac)
Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.

Choose the Cross-platform App project under Visual C#-->Cross-platform in the "New Project" dialog.

 

Now, select the Blank App and choose Portable Class Library (PCL).
 
 

Subsequently, go to the Solution. In there, you get all the files and sources of your project (PCL). Now, select XAML page and double-click to open the MainPage.Xaml page.

You now have a basic Xamarin.Forms app. Click the Play button to try it out.


In this step, create a default Editor control with border.

Now, write the following code.

MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinFormsEditor" x:Class="XamarinFormsEditor.MainPage">  
  3.     <ContentPage.Content>  
  4.         <StackLayout>  
  5.             <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="StartAndExpand">  
  6.                 <Image x:Name="imgBanner" HeightRequest="300" WidthRequest="300"></Image>  
  7.                 <Editor Text="Enter Some text here..." HeightRequest="100" BackgroundColor="LightGray"></Editor>  
  8.             </StackLayout>  
  9.         </StackLayout>  
  10.     </ContentPage.Content>  
  11. </ContentPage>  
Click the Play button to try it out.

 
 
Create a Custom Editor
 
Now, create an Inherit class form Editor for Customizing the Editor control.

Go to Solution—>PCL—>Right click—>New—>Class—>XEditor.cs.
 
 
 
Now, write the following code.

XEditor.cs

  1. using Xamarin.Forms;  
  2. namespace XamarinFormsEditor.Controls  
  3.    {  
  4.       public class XEditor:Editor  
  5.    {  
  6.    }  

Making Your Android Implementation

In this step, create an inherit Class form, EditorRenderer for customizing the Editor control.

Go to Solution.Droid—>Class—> XEditorRenderer.cs.

 
 
Now, write the code given below.
 
XEditorRenderer.cs
  1. using Xamarin.Forms;  
  2. using XamarinFormsEditor.Controls;  
  3. using XamarinFormsEditor.Droid.Helpers;  
  4. using Xamarin.Forms.Platform.Android;  
  5. using Android.Graphics.Drawables;  
  6. [assembly: ExportRenderer(typeof(XEditor), typeof(XEditorRenderer))]  
  7. namespace XamarinFormsEditor.Droid.Helpers {  
  8.     public class XEditorRenderer: EditorRenderer {  
  9.         protected override void OnElementChanged(ElementChangedEventArgs < Editor > e) {  
  10.             base.OnElementChanged(e);  
  11.             if (Control != null) {  
  12.                 Control.Background = new ColorDrawable(Android.Graphics.Color.Transparent);  
  13.             }  
  14.         }  
  15.     }  
  16. }  
 
Setting up the User Interface
 
Now, add a Customized Editor control to your app. Go to MainPage.Xaml and write the following code.
 
MainPage.Xaml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinFormsEditor" xmlns:Controls="clr-namespace:XamarinFormsEditor.Controls" x:Class="XamarinFormsEditor.MainPage">  
  3.     <ContentPage.Content>  
  4.         <StackLayout>  
  5.             <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="StartAndExpand">  
  6.                 <Image x:Name="imgBanner" HeightRequest="300" WidthRequest="300"></Image>  
  7.                 <Controls:XEditor Text="Enter Some text here..." HeightRequest="100" BackgroundColor="LightGray"></Controls:XEditor>  
  8.             </StackLayout>  
  9.         </StackLayout>  
  10.     </ContentPage.Content>  
  11. </ContentPage>  
Go to MainPage.Xaml.cs and write the following code (optional).
 

 
MainPage.Xaml.cs
  1. using Xamarin.Forms;  
  2. namespace XamarinFormsEditor {  
  3.     public partial class MainPage: ContentPage {  
  4.         public MainPage() {  
  5.             InitializeComponent();  
  6.             imgBanner.Source = ImageSource.FromResource("XamarinFormsEditor.images.banner.png");  
  7.         }  
  8.     }  
  9. }  
 
 
Click the Play button to try it out.

 

I hope you have understood how to create a Borderless Editor using Custom Renderer.

Thanks for reading. Please share your comments and feedback.


Similar Articles