Search This Blog

Thursday 5 November 2020

Borderless Entry Control in Xamarin Forms



Implementation (LoginPage.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:renderer="clr-namespace:TestApp.Renderer"
             x:Class="TestApp.Views.Login.LoginPage">
    <ContentPage.Content>
        <Grid Margin="10,70,10,10" HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <renderer:BorderlessEntry Text="This is Borderless entry" Grid.Row="0" />
        </Grid>
    </ContentPage.Content>
</ContentPage>

XamarinForm Renderer (BorderlessEntry.cs)

Create Custom Renderer in your Xamarin Form Project

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
 
namespace TestApp.Renderer
{
    public class BorderlessEntry : Entry
    {
    }
}

Create Platform Specific Renderer for iOS & Android Project that Export  BorderlessEntry.cs Renderer(From Xamarin Form Project)

Xamarin.Android Renderer (BorderlessEntryRenderer.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Text;
using Android.Views;
using Android.Widget;
using TestApp.Droid.CustomRenderers;
using TestApp.Renderer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
 
[assembly: ExportRenderer(typeof(BorderlessEntry), typeof(BorderlessEntryRenderer))]
namespace TestApp.Droid.CustomRenderers
{
    public class BorderlessEntryRenderer : EntryRenderer
    {
        public BorderlessEntryRenderer(Context context):base(context)
        {
 
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null && Element != null)
            {
                Control.Background = null;
            }
        }
    }
}

Xamarin.ios Renderer (BorderlessEntryRenderer.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestApp.iOS.CustomRenderers;
using TestApp.Renderer;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
 
[assembly: ExportRenderer(typeof(BorderlessEntry), typeof(BorderlessEntryRenderer))]
namespace TestApp.iOS.CustomRenderers
{
    public class BorderlessEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
 
            if (Control != null && Element != null)
            {
                Control.BackgroundColor = Color.Transparent.ToUIColor();
                Control.Layer.BackgroundColor = Color.Transparent.ToCGColor();
                Control.BorderStyle = UITextBorderStyle.None;
            }
        }
    }
}
 

No comments:

Post a Comment

Popular Posts