Introduction
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem.
This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute 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 or later (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.
Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.
Now, you need to click "Create a new project".
Now, filter by Project Type: Mobile
Choose the Mobile App (Xamarin. forms) project under C# and Mobile.
Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".
Now, select the blank app and target platforms - Android, iOS and Windows (UWP).
Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the 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.
Create a Custom Entry
Now, create an Inherit class form Entry for Customizing the Entry control.
Write the following code.
CustomEntry.cs
- using Xamarin.Forms;
-
- namespace XamarinEntry
- {
- public class CustomEntry:Entry
- {
-
- }
- }
Android Implementation
In this step, create an inherit Class form, EntryRenderer for customizing the Entry control.
Now, write the code given below.
CustomEntryRenderer.cs
- using Xamarin.Forms;
- using Xamarin.Forms.Platform.Android;
- using XamarinEntry;
- using XamarinEntry.Droid;
-
- [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
- namespace XamarinEntry.Droid
- {
- public class CustomEntryRenderer: EntryRenderer
- {
- public CustomEntryRenderer(Context context) : base(context)
- {
- AutoPackage = false;
- }
- protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
- {
- base.OnElementChanged(e);
- if (Control != null)
- {
- Control.Background = new ColorDrawable(Android.Graphics.Color.Transparent);
- }
- }
- }
- }
iOS Implementation
In this step, create an inherit Class form, EntryRenderer for customizing the Entry control.
Now, write the code given below.
CustomEntryRenderer.cs
- using UIKit;
- using Xamarin.Forms;
- using Xamarin.Forms.Platform.iOS;
- using XamarinEntry;
- using XamarinEntry.iOS;
-
- [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
- namespace XamarinEntry.iOS
- {
- public class CustomEntryRenderer: EntryRenderer
- {
- protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- base.OnElementPropertyChanged(sender, e);
-
- Control.Layer.BorderWidth = 0;
- Control.BorderStyle = UITextBorderStyle.None;
- }
- }
- }
Setting up the User Interface
Go to MainPage.Xaml and write the following code.
MainPage.xaml
- <?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:XamarinEntry"
- xmlns:controls="clr-namespace:XamarinEntry"
- x:Class="XamarinEntry.MainPage">
- <!--Common Styles Starts-->
- <ContentPage.Resources>
- <ResourceDictionary>
-
- <Color x:Key="LightGreenColor">#2FA999</Color>
- <Color x:Key="BorderColor">#D8D8D8</Color>
-
- <Style x:Key="LableStyle" TargetType="Label">
- <Setter Property="TextColor" Value="#666666" />
- <Setter Property="FontSize" Value="Large" />
- </Style>
- <Style x:Key="FrameStyle" TargetType="Frame">
- <Setter Property="HasShadow" Value="False" />
- <Setter Property="Padding" Value="0" />
- <Setter Property="CornerRadius" Value="5" />
- <Setter Property="BorderColor" Value="{StaticResource BorderColor}" />
- </Style>
- <Style x:Key="EntryStyle" TargetType="Entry">
- <Setter Property="HeightRequest" Value="40"></Setter>
- <Setter Property="Margin" Value="5,0,0,0"></Setter>
- </Style>
- </ResourceDictionary>
- </ContentPage.Resources>
- <!--Common Styles Ends-->
- <StackLayout Margin="20,0" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
- <Image Margin="0,0,0,30" Source="banner1"></Image>
- <Frame HasShadow="False" CornerRadius="5" BorderColor="{StaticResource BorderColor}" BackgroundColor="#F7F7F7">
-
- <StackLayout>
- <Label Style="{StaticResource LableStyle}" Text="ShopId" />
- <Frame Style="{StaticResource FrameStyle}">
- <controls:CustomEntry x:Name="txtShopId" Keyboard="Numeric" Style="{StaticResource EntryStyle}"></controls:CustomEntry>
- </Frame>
- <Label Style="{StaticResource LableStyle}" Text="UserId" />
- <Frame Style="{StaticResource FrameStyle}">
- <controls:CustomEntry x:Name="txtUserId" Keyboard="Numeric" Style="{StaticResource EntryStyle}"></controls:CustomEntry>
- </Frame>
- <Label Style="{StaticResource LableStyle}" Text="Password"></Label>
- <Frame Style="{StaticResource FrameStyle}">
- <controls:CustomEntry IsPassword="True" x:Name="txtPassword" Style="{StaticResource EntryStyle}"></controls:CustomEntry>
- </Frame>
- <Button Margin="100,20" BorderRadius="6" WidthRequest="200" x:Name="ShopIDSubmit" TextColor="White" BackgroundColor="{StaticResource LightGreenColor}" Text="Login"></Button>
- </StackLayout>
- </Frame>
- </StackLayout>
-
- </ContentPage>
Click the "Play" button to try it out.
I hope you have understood how to create a borderless entry with rounded corners in Xamarin.Forms.
Thanks for reading. Please share your comments and feedback. Happy Coding :)