【问题标题】:How to disable MaterialPickerRenderer from changing underline opacity in Xamarin.Forms Material Visual如何禁用 MaterialPickerRenderer 在 Xamarin.Forms Material Visual 中更改下划线不透明度
【发布时间】:2020-03-23 11:35:25
【问题描述】:

我创建了一个自定义渲染器来扩展 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)
            );
        }
    }
}

任何建议将不胜感激 - 谢谢!

【问题讨论】:

    标签: c# xamarin xamarin.forms xamarin.ios material-design


    【解决方案1】:

    尝试改为设置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)
                );
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-11
      • 2012-07-23
      • 2016-10-19
      • 2013-03-04
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      相关资源
      最近更新 更多