xamarin条目无效

xamarin条目无效,xamarin,xamarin.forms,xamarin.forms.entry,Xamarin,Xamarin.forms,Xamarin.forms.entry,我在xamarin.forms应用程序的一个页面中有一个条目在我的平板电脑上无法正常工作。当我单击该条目时,会出现键盘,我可以键入,但只能显示占位符。该应用程序可在其他android设备上运行,但我的平板电脑不显示输入数据。 平板电脑:galaxy tab s-安卓5.0 电话:galaxy s6-安卓6.0 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.micr

我在xamarin.forms应用程序的一个页面中有一个条目在我的平板电脑上无法正常工作。当我单击该条目时,会出现键盘,我可以键入,但只能显示占位符。该应用程序可在其他android设备上运行,但我的平板电脑不显示输入数据。 平板电脑:galaxy tab s-安卓5.0 电话:galaxy s6-安卓6.0

<ContentPage
  xmlns="http://xamarin.com/schemas/2014/forms"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  x:Class="MyProject.MyPage"
  Title="SomeTitle"
  Padding="10, 40, 10, 10">
  <Entry Placeholder="Hi"/>
</ContentPage>

在一些三星设备中,如果您需要编写自定义渲染器并尝试设置文本颜色或背景颜色,则会出现这种情况


在一些三星设备中,如果您需要编写自定义渲染器并尝试设置文本颜色或背景颜色,则会出现这种情况

试试这个

用法:

ExtendedEntry entry = new ExtendedEntry
{
     Text = "Dummy Text",
     TextColor = Color.Black,
     PlaceholderTextColor = Color.Black,
};
PCL代码

using Xamarin.Forms;

namespace VHS.MobileApp.eVita.CustomViews
{
    public class ExtendedEntry : Entry
    {
        /// <summary>
        /// The font property
        /// </summary>
        public static readonly BindableProperty FontProperty =
            BindableProperty.Create("Font", typeof(Font), typeof(ExtendedEntry), new Font());

        /// <summary>
        /// The XAlign property
        /// </summary>
        public static readonly BindableProperty XAlignProperty =
            BindableProperty.Create("XAlign", typeof(TextAlignment), typeof(ExtendedEntry),
            TextAlignment.Start);

        /// <summary>
        /// The HasBorder property
        /// </summary>
        public static readonly BindableProperty HasBorderProperty =
            BindableProperty.Create("HasBorder", typeof(bool), typeof(ExtendedEntry), true);

        /// <summary>
        /// The PlaceholderTextColor property
        /// </summary>
        public static readonly BindableProperty PlaceholderTextColorProperty =
            BindableProperty.Create("PlaceholderTextColor", typeof(Color), typeof(ExtendedEntry), Color.Default);

        /// <summary>
        /// The MaxLength property
        /// </summary>
        public static readonly BindableProperty MaxLengthProperty =
            BindableProperty.Create("MaxLength", typeof(int), typeof(ExtendedEntry), int.MaxValue);

        /// <summary>
        /// Gets or sets the MaxLength
        /// </summary>
        public int MaxLength
        {
            get { return (int)this.GetValue(MaxLengthProperty); }
            set { this.SetValue(MaxLengthProperty, value); }
        }

        /// <summary>
        /// Gets or sets the Font
        /// </summary>
        public Font Font
        {
            get { return (Font)GetValue(FontProperty); }
            set { SetValue(FontProperty, value); }
        }

        /// <summary>
        /// Gets or sets the X alignment of the text
        /// </summary>
        public TextAlignment XAlign
        {
            get { return (TextAlignment)GetValue(XAlignProperty); }
            set { SetValue(XAlignProperty, value); }
        }

        /// <summary>
        /// Gets or sets if the border should be shown or not
        /// </summary>
        public bool HasBorder
        {
            get { return (bool)GetValue(HasBorderProperty); }
            set { SetValue(HasBorderProperty, value); }
        }

        /// <summary>
        /// Sets color for placeholder text
        /// </summary>
        public Color PlaceholderTextColor
        {
            get { return (Color)GetValue(PlaceholderTextColorProperty); }
            set { SetValue(PlaceholderTextColorProperty, value); }
        }

        public static readonly BindableProperty BorderRadiusProperty =
        BindableProperty.Create<ExtendedEntry, float>(p => p.BorderRadius, default(float));

        public float BorderRadius
        {
            get { return (float)GetValue(BorderRadiusProperty); }
            set { SetValue(BorderRadiusProperty, value); }
        }

    }
}
使用Xamarin.Forms;
命名空间VHS.MobileApp.eVita.CustomViews
{
公共类ExtendedEntry:Entry
{
/// 
///字体属性
/// 
公共静态只读BindableProperty FontProperty=
创建(“Font”、typeof(Font)、typeof(ExtendedEntry)、newfont());
/// 
///XAlign属性
/// 
公共静态只读BindableProperty XAlignProperty=
创建(“XAlign”、typeof(TextAlignment)、typeof(ExtendedEntry),
TextAlignment.Start);
/// 
///HasBorder属性
/// 
公共静态只读BindableProperty HasBorderProperty=
Create(“HasBorder”、typeof(bool)、typeof(ExtendedEntry)、true);
/// 
///占位符TextColor属性
/// 
公共静态只读BindableProperty占位符TextColorProperty=
创建(“占位符TextColor”、typeof(颜色)、typeof(ExtendedEntry)、Color.Default);
/// 
///MaxLength属性
/// 
公共静态只读BindableProperty MaxLengthProperty=
创建(“MaxLength”、typeof(int)、typeof(ExtendedEntry)、int.MaxValue);
/// 
///获取或设置MaxLength
/// 
公共整数最大长度
{
获取{return(int)this.GetValue(MaxLengthProperty);}
set{this.SetValue(MaxLengthProperty,value);}
}
/// 
///获取或设置字体
/// 
公共字体
{
获取{return(Font)GetValue(FontProperty);}
集合{SetValue(FontProperty,value);}
}
/// 
///获取或设置文本的X对齐方式
/// 
公共文本对齐XAlign
{
获取{return(TextAlignment)GetValue(XAlignProperty);}
set{SetValue(XAlignProperty,value);}
}
/// 
///获取或设置是否应显示边框
/// 
公共边界
{
获取{return(bool)GetValue(HasBorderProperty);}
set{SetValue(HasBorderProperty,value);}
}
/// 
///设置占位符文本的颜色
/// 
公共颜色占位符文本颜色
{
获取{return(Color)GetValue(PlaceholderTextColorProperty);}
set{SetValue(占位符TextColorProperty,value);}
}
公共静态只读BindableProperty BorderRadiusProperty=
Create(p=>p.BorderRadius,默认值(float));
公共浮动边界半径
{
获取{return(float)GetValue(BorderRadiusProperty);}
set{SetValue(BorderRadiusProperty,value);}
}
}
}
机器人代码:

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.Views;
using Android.Widget;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using VHS.MobileApp.eVita.CustomViews;
using Android.Graphics;
using Android.Text.Method;
using Android.Util;
using Color = Xamarin.Forms.Color;
using VHS.MobileApp.eVita.Droid.CustomRenderer;
using System.ComponentModel;
using Android.Text;


[assembly: ExportRenderer(typeof(ExtendedEntry), typeof(ExtendedEntryRenderer))]
namespace VHS.MobileApp.eVita.Droid.CustomRenderer
{

    public class ExtendedEntryRenderer : EntryRenderer
    {

        protected ExtendedEntryRenderer(IntPtr javaReference, JniHandleOwnership transfer)
        {

        }

        public ExtendedEntryRenderer()
            : base()
        {

        }

        private const int MinDistance = 10;

        /// <summary>
        /// Called when [element changed].
        /// </summary>
        /// <param name="e">The e.</param>
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            var view = (ExtendedEntry)Element;

            if (Control != null && e.NewElement != null && e.NewElement.IsPassword)
            {
                Control.SetTypeface(Typeface.Default, TypefaceStyle.Normal);
                Control.TransformationMethod = new PasswordTransformationMethod();
                this.Control.SetBackgroundColor(Android.Graphics.Color.White);
            }


            #region SETTING CUSTOM FONT

            if (view.FontFamily != null && view.FontFamily != string.Empty)
            {
                Typeface typeface = Typeface.CreateFromAsset(Forms.Context.Assets, view.FontFamily);
                Control.Typeface = typeface;
            }

            #endregion

            // SetFont(view);
            SetTextAlignmentWithVerticalFill(view);
            SetPlaceholderTextColor(view);
            SetMaxLength(view);
        }

        /// <summary>
        /// Handles the touch.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Android.Views.View.TouchEventArgs"/> instance containing the event data.</param>
        /*void HandleTouch(object sender, TouchEventArgs e)
        {
            var element = (ExtendedEntry)this.Element;
            switch (e.Event.Action)
            {
                case MotionEventActions.Down:
                    this.downX = e.Event.GetX();
                    this.downY = e.Event.GetY();
                    return;
                case MotionEventActions.Up:
                case MotionEventActions.Cancel:
                case MotionEventActions.Move:
                    this.upX = e.Event.GetX();
                    this.upY = e.Event.GetY();

                    float deltaX = this.downX - this.upX;
                    float deltaY = this.downY - this.upY;

                    // swipe horizontal?
                    if (Math.Abs(deltaX) > Math.Abs(deltaY))
                    {
                        if (Math.Abs(deltaX) > MinDistance)
                        {
                            if (deltaX < 0)
                            {
                                element.OnRightSwipe(this, EventArgs.Empty);
                                return;
                            }

                            if (deltaX > 0)
                            {
                                element.OnLeftSwipe(this, EventArgs.Empty);
                                return;
                            }
                        }
                        else
                        {
                            Log.Info("ExtendedEntry", "Horizontal Swipe was only " + Math.Abs(deltaX) + " long, need at least " + MinDistance);
                            return; // We don't consume the event
                        }
                    }
                    // swipe vertical?
                    //                    else 
                    //                    {
                    //                        if(Math.abs(deltaY) > MIN_DISTANCE){
                    //                            // top or down
                    //                            if(deltaY < 0) { this.onDownSwipe(); return true; }
                    //                            if(deltaY > 0) { this.onUpSwipe(); return true; }
                    //                        }
                    //                        else {
                    //                            Log.i(logTag, "Vertical Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                    //                            return false; // We don't consume the event
                    //                        }
                    //                    }

                    return;
            }
        }*/

        /// <summary>
        /// Handles the <see cref="E:ElementPropertyChanged" /> event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var view = (ExtendedEntry)Element;
            this.Control.SetBackgroundColor(Android.Graphics.Color.White);
            //if (e.PropertyName == ExtendedEntry.FontProperty.PropertyName)
            //{
            //    SetFont(view);
            //}
            //else

            if (e.PropertyName == ExtendedEntry.XAlignProperty.PropertyName)
            {
                SetTextAlignmentWithVerticalFill(view);
            }
            else if (e.PropertyName == ExtendedEntry.HasBorderProperty.PropertyName)
            {
                //return;   
            }
            else if (e.PropertyName == ExtendedEntry.PlaceholderTextColorProperty.PropertyName)
            {
                SetPlaceholderTextColor(view);
            }
            else
            {
                base.OnElementPropertyChanged(sender, e);
                if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
                {
                    this.Control.SetBackgroundColor(view.BackgroundColor.ToAndroid());
                }
            }
        }

        /// <summary>
        /// Sets the text alignment.
        /// </summary>
        /// <param name="view">The view.</param>
        private void SetTextAlignmentWithVerticalFill(ExtendedEntry entry)
        {
            switch (entry.XAlign)
            {
                case Xamarin.Forms.TextAlignment.Center:
                    Control.Gravity = GravityFlags.Center;
                    break;
                case Xamarin.Forms.TextAlignment.End:
                    Control.Gravity = GravityFlags.End | GravityFlags.CenterVertical;
                    break;
                case Xamarin.Forms.TextAlignment.Start:
                    Control.Gravity = GravityFlags.Start | GravityFlags.CenterVertical;
                    break;
            }
        }

        /// <summary>
        /// Sets the color of the placeholder text.
        /// </summary>
        /// <param name="view">The view.</param>
        private void SetPlaceholderTextColor(ExtendedEntry view)
        {
            if (view.PlaceholderTextColor != Color.Default)
            {
                Control.SetHintTextColor(view.PlaceholderTextColor.ToAndroid());
            }
        }

        /// <summary>
        /// Sets the MaxLength characteres.
        /// </summary>
        /// <param name="view">The view.</param>
        private void SetMaxLength(ExtendedEntry view)
        {
            Control.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(view.MaxLength) });
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Xamarin.Forms.Platform.Android;
使用Xamarin.Forms;
使用VHS.MobileApp.eVita.customview;
使用Android.Graphics;
使用Android.Text.Method;
使用Android.Util;
使用Color=Xamarin.Forms.Color;
使用VHS.MobileApp.eVita.Droid.CustomRenderer;
使用系统组件模型;
使用Android.Text;
[程序集:ExportRenderer(typeof(ExtendedEntry)、typeof(ExtendedEntryRenderer))]
命名空间VHS.MobileApp.eVita.Droid.CustomRenderer
{
公共类ExtendedEntryRenderer:EntryRenderer
{
受保护的ExtendedEntryRenderer(IntPtr javaReference、JniHandleOwnership transfer)
{
}
公共ExtendedEntryRenderer()
:base()
{
}
私人常数int MinDistance=10;
/// 
///在[element changed]时调用。
/// 
///e。
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
变量视图=(ExtendedEntry)元素;
if(Control!=null&&e.NewElement!=null&&e.NewElement.IsPassword)
{
Control.SetTypeface(Typeface.Default,TypefaceStyle.Normal);
Control.TransformationMethod=新密码TransformationMethod();
this.Control.SetBackgroundColor(Android.Graphics.Color.White);
}
#区域设置自定义字体
if(view.FontFamily!=null&&view.FontFamily!=string.Empty)
{
Typeface-Typeface=Typeface.CreateFromAsset(Forms.Context.Assets,view.FontFamily);
Control.Typeface=字体;
}
#端区
//SetFont(视图);
SetTextAlignmentWithVerticalFill(视图);
设置占位符文本颜色(视图);
SetMaxLength(视图);
}
/// 
///处理触摸。
/// 
///发送者。
///包含事件数据的实例。
/*void HandleTouch(对象发送器,TouchEventArgs e)
{
var元素=(Exte)