csharp Xamarin.Forms条目只有底部边框。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Xamarin.Forms条目只有底部边框。相关的知识,希望对你有一定的参考价值。

using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using UIKit;
using YOUTNAMESPACE.iOS;
using System.ComponentModel;
using CoreAnimation;
using Foundation;

[assembly: ExportRenderer (typeof(YOUTNAMESPACE.LineEntry), typeof(LineEntryRenderer))]
namespace YOUTNAMESPACE.iOS
{
	public class LineEntryRenderer: EntryRenderer
	{
		protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
		{
			base.OnElementChanged (e);

			if (Control != null) {
				Control.BorderStyle = UITextBorderStyle.None;

				var view = (Element as LineEntry);
				if (view != null) {
					DrawBorder (view);
					SetFontSize (view);
					SetPlaceholderTextColor (view);
				}
			}
		}

		protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged (sender, e);

			var view = (LineEntry)Element;

			if (e.PropertyName.Equals (view.BorderColor))
				DrawBorder (view);
			if (e.PropertyName.Equals (view.FontSize))
				SetFontSize (view);
			if (e.PropertyName.Equals (view.PlaceholderColor))
				SetPlaceholderTextColor (view);
		}

		void DrawBorder (LineEntry view)
		{
			var borderLayer = new CALayer ();
			borderLayer.MasksToBounds = true;
			borderLayer.Frame = new CoreGraphics.CGRect (0f, Frame.Height / 2, Frame.Width, 1f);
			borderLayer.BorderColor = view.BorderColor.ToCGColor ();
			borderLayer.BorderWidth = 1.0f;

			Control.Layer.AddSublayer (borderLayer);
			Control.BorderStyle = UITextBorderStyle.None;
		}

		void SetFontSize (LineEntry view)
		{
			if (view.FontSize != Font.Default.FontSize)
				Control.Font = UIFont.SystemFontOfSize ((System.nfloat)view.FontSize);
			else if (view.FontSize == Font.Default.FontSize)
				Control.Font = UIFont.SystemFontOfSize (17f);
		}

		void SetPlaceholderTextColor (LineEntry view)
		{
			if (string.IsNullOrEmpty (view.Placeholder) == false && view.PlaceholderColor != Color.Default) {
				var placeholderString = new NSAttributedString (view.Placeholder, 
					                        new UIStringAttributes { ForegroundColor = view.PlaceholderColor.ToUIColor () });
				Control.AttributedPlaceholder = placeholderString;
			}
		}
	}
}

using Xamarin.Forms;

namespace YOUTNAMESPACE
{
	public class LineEntry : Entry
	{
		public static readonly BindableProperty BorderColorProperty = 
			BindableProperty.Create<LineEntry, Color> (p => p.BorderColor, Color.Black);

		public Color BorderColor {
			get { return (Color)GetValue (BorderColorProperty); }
			set { SetValue (BorderColorProperty, value); }
		}

		public static readonly BindableProperty FontSizeProperty = 
			BindableProperty.Create<LineEntry, double> (p => p.FontSize, Font.Default.FontSize);

		public double FontSize {
			get { return (double)GetValue (FontSizeProperty); }
			set { SetValue (FontSizeProperty, value); }
		}

		public static readonly BindableProperty PlaceholderColorProperty =
			BindableProperty.Create<LineEntry, Color> (p => p.PlaceholderColor, Color.Default);

		public Color PlaceholderColor {
			get { return (Color)GetValue (PlaceholderColorProperty); }
			set { SetValue (PlaceholderColorProperty, value); }
		}


	}
}

以上是关于csharp Xamarin.Forms条目只有底部边框。的主要内容,如果未能解决你的问题,请参考以下文章

在 Xamarin.Forms 条目视图中使用仅英文键盘

csharp MyBindableCustomPicker / Xamarin.Forms

Xamarin.Forms 仅在一个平台 (iOS) 上更改条目的占位符颜色和文本颜色

Xamarin.Forms 条目 - 自定义行为和 MVVM

不应在其焦点上显示软键盘的 Xamarin.Forms 条目

Xamarin Forms WinRT 条目自定义渲染器