c# - 如何禁用 MaterialPickerRenderer 在 Xamarin.Forms Material Visual 中更改下划线不透明度
问题描述
我创建了一个自定义渲染器来扩展Xamarin.Forms 中可用的 MaterialPickerRenderer 。
MaterialPickerRenderer的默认行为是渲染不透明的下划线(见下图)。
在我的自定义渲染器中,我试图禁用此行为 - 我不希望下划线不透明。In my custom renderer for iOS, I have been able to set the initial underline color, however, the color is updated to an opaque color when the picker loses focus (see gif below).
我现在只关注 iOS 的渲染器。我想知道是否有人对如何覆盖和禁用此默认 MaterialPickerRenderer 行为有任何建议。下面是我的自定义渲染器代码:
using System;
using Foundation;
using Solstice.Extensions;
using Solstice.iOS.Renderers;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Material.iOS;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Picker), typeof(TitledMaterialPickerRenderer), new[] { typeof(CustomVisual) })]
namespace Solstice.iOS.Renderers
{
public class TitledMaterialPickerRenderer : MaterialPickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Underline.Color = UIColor.Clear.FromHex(0x46433E);
}
}
}
public static class UIColorExtensions
{
public static UIColor FromHex(this UIColor color, int hexValue)
{
return UIColor.FromRGB(
(((float)((hexValue & 0xFF0000) >> 16)) / 255.0f),
(((float)((hexValue & 0xFF00) >> 8)) / 255.0f),
(((float)(hexValue & 0xFF)) / 255.0f)
);
}
}
}
任何建议将不胜感激 - 谢谢!
解决方案
尝试设置Control.Underline.Layer.BorderColor
:
[assembly: ExportRenderer(typeof(Picker), typeof(TitledMaterialPickerRenderer), new[] { typeof(VisualMarker.MaterialVisual) })]
namespace VisualDemos.iOS
{
public class TitledMaterialPickerRenderer : MaterialPickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Underline.Layer.BorderColor = UIColor.Clear.FromHex(0x46433E).CGColor;
Control.Underline.Layer.BorderWidth = 1;
}
}
}
public static class UIColorExtensions
{
public static UIColor FromHex(this UIColor color, int hexValue)
{
return UIColor.FromRGB(
(((float)((hexValue & 0xFF0000) >> 16)) / 255.0f),
(((float)((hexValue & 0xFF00) >> 8)) / 255.0f),
(((float)(hexValue & 0xFF)) / 255.0f)
);
}
}
}