我有一个问题,我无法解决。也许你可以帮助我。我想为xamarin表单中的条目设置灵活的下划线。当我将customEntry的BackgroundColor设置为透明时,下划线消失,但我无法带来"它回来了。我已经尝试过一些东西,比如设置新的边界,这些东西不适用于底部或设置新的BackgroundColor。谷歌也无法帮助我。 你有什么想法可以解决这个问题吗?
谢谢
我的PCL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace MultiBaseCS_Mobile
{
public class CustomEntryCell : Entry
{
public static readonly BindableProperty HasUnderlineProperty =
BindableProperty.Create(nameof(HasUnderline), typeof(bool), typeof(CustomEntryCell), true);
public CustomEntryCell()
{
}
public bool HasUnderline
{
get { return (bool)GetValue(HasUnderlineProperty); }
set { SetValue(HasUnderlineProperty, value); }
}
}
}
我的customRenderer
using Xamarin.Forms;
using MultiBaseCS_Mobile;
using MultiBaseCS_Mobile.Droid;
using Xamarin.Forms.Platform.Android;
using Android.Text;
using System.ComponentModel;
using Android.Graphics.Drawables;
using Android.Graphics;
[assembly: ExportRenderer(typeof(CustomEntryCell), typeof(CustomEntryRenderer))]
namespace MultiBaseCS_Mobile.Droid
{
public class CustomEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
var entry = (CustomEntryCell)Element;
if (entry != null)
{
if (!entry.HasUnderline)
{
Control.SetBackgroundColor(Android.Graphics.Color.Argb(0, 0, 0, 0));
}
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var entry = (CustomEntryCell)Element;
if (e.PropertyName == CustomEntryCell.HasUnderlineProperty.PropertyName)
{
if (!entry.HasUnderline)
{
Control.SetBackgroundColor(Android.Graphics.Color.Argb(0, 0, 0, 0));
}
else
{
// Underline should appear
}
}
}
}
}
答案 0 :(得分:0)
以下是您可以尝试的内容
using Xamarin.Forms;
using MultiBaseCS_Mobile;
using MultiBaseCS_Mobile.Droid;
using Xamarin.Forms.Platform.Android;
using Android.Text;
using System.ComponentModel;
using Android.Graphics.Drawables;
using Android.Graphics;
[assembly: ExportRenderer(typeof(CustomEntryCell), typeof(CustomEntryRenderer))]
namespace MultiBaseCS_Mobile.Droid
{
public class CustomEntryRenderer : EntryRenderer
{
InsetDrawable originalBackgroundDrawable = null;
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
originalBackgroundDrawable = Control.Background as InsetDrawable;
var entry = (CustomEntryCell)Element;
if (entry != null)
{
if (!entry.HasUnderline)
{
Control.SetBackground(null);
}
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var entry = (CustomEntryCell)Element;
if (e.PropertyName == CustomEntryCell.HasUnderlineProperty.PropertyName)
{
if (!entry.HasUnderline)
{
Control.SetBackgroundColor(null);
}
else
{
// Underline should appear
Control.SetBackgroundColor(originalBackgroundDrawable);
}
}
}
}
}