[Xamarin] Entry 에서 언더라인 제거 하기
Entry 의 언더라인은 디자인적으로 안보이는게 깔끔할때가 있습니다.
이를 제거하는 방법입니다.
(참고로 iOS 는 차이가 없습니다.^^;)
.Net Standard 프로젝트에 CustomEntry.cs 를 작성합니다.
using Xamarin.Forms;
namespace Test.Cntrols
{
public class CustomEntry : Entry
{
public static readonly BindableProperty HasBorderProperty =
BindableProperty.Create(nameof(HasBorder), typeof(bool), typeof(CustomEntry), true);
public bool HasBorder
{
get { return (bool)GetValue(HasBorderProperty); }
set { SetValue(HasBorderProperty, value); }
}
public CustomEntry() : base()
{
Margin = 2;
}
}
}
Xamarin.Android 프로젝트에 CustomEntryRenderer.cs 를 작성합니다.
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Test.Cntrols;
using Test.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace Test.Droid
{
public class CustomEntryRenderer : EntryRenderer
{
public CustomEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
if (e.NewElement != null)
{
var customEntry = (CustomEntry)e.NewElement;
if (!customEntry.HasBorder)
{
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.SetColor(Android.Graphics.Color.Transparent);
Control.SetBackground(gradientDrawable);
}
}
}
}
}
}
Xamarin.iOS 프로젝트에도 CustomEntryRenderer.cs 를 작성합니다.
using UIKit;
using Test.Cntrols;
using Test.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace Test.iOS
{
class CustomEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
if (e.NewElement != null)
{
var customEntry = (CustomEntry)e.NewElement;
if (!customEntry.HasBorder)
{
Control.BorderStyle = UITextBorderStyle.None;
}
}
}
}
}
}
화면(xaml)에 아래처럼 작성합니다.
<cntrols:CustomEntry x:Name="testEntry"
HasBorder="False"
Placeholder = "언더라인제거Entry"
PlaceholderColor="#95969B"/>
결과
일반적인 Entry 와
언더라인을 없앤 CustomEnrty