Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义渲染器不渲染Xamarin表单_C#_Xaml_Xamarin_Xamarin.forms - Fatal编程技术网

C# 自定义渲染器不渲染Xamarin表单

C# 自定义渲染器不渲染Xamarin表单,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我正在尝试在我的Xamarin表单项目(PCL)中实现一个自定义渲染器。我正在尝试弯曲标签的角。 问题是它没有渲染。我在相关文件中抛出了一个调试,它们被命中了,但它们似乎没有被渲染 下面是标签的子类: public class CurvedCornersLabel:Label { public static readonly BindableProperty CurvedCornerRadiusProperty = BindableProperty.Create(

我正在尝试在我的Xamarin表单项目(PCL)中实现一个自定义渲染器。我正在尝试弯曲标签的角。 问题是它没有渲染。我在相关文件中抛出了一个调试,它们被命中了,但它们似乎没有被渲染

下面是标签的子类:

public class CurvedCornersLabel:Label
{
    public static readonly BindableProperty CurvedCornerRadiusProperty =
         BindableProperty.Create(
            nameof(CurvedCornerRadius),
            typeof(double),
            typeof(CurvedCornersLabel),
            12.0);

    public double CurvedCornerRadius
    {
        get { return (double)GetValue(CurvedCornerRadiusProperty); }
        set { SetValue(CurvedCornerRadiusProperty, value); }
    }


    public static readonly BindableProperty CurvedBackgroundColorProperty =
        BindableProperty.Create(
            nameof(CurvedCornerRadius),
            typeof(Color),
            typeof(CurvedCornersLabel),
            Color.Default);
    public Color CurvedBackgroundColor
    {
        get { return (Color)GetValue(CurvedBackgroundColorProperty); }
        set { SetValue(CurvedBackgroundColorProperty, value); }
    }
}
这是Android的渲染器:

    [assembly: ExportRenderer(typeof(CurvedCornersLabel), typeof(CurvedCornerLabelRenderer))]
namespace CarouselDemo.Droid
{
    public class CurvedCornerLabelRenderer:LabelRenderer
    {
        private GradientDrawable _gradientBackground;

        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            var view = (CurvedCornersLabel)Element;
            if (view == null) return;

            // creating gradient drawable for the curved background
            _gradientBackground = new GradientDrawable();
            _gradientBackground.SetShape(ShapeType.Rectangle);
            _gradientBackground.SetColor(view.CurvedBackgroundColor.ToAndroid());

            // Thickness of the stroke line
            _gradientBackground.SetStroke(4, view.CurvedBackgroundColor.ToAndroid());

            // Radius for the curves
            _gradientBackground.SetCornerRadius(
                DpToPixels(this.Context,
                Convert.ToSingle(view.CurvedCornerRadius)));

            // set the background of the label
            Control.SetBackground(_gradientBackground);
        }

        /// <summary>
        /// Device Independent Pixels to Actual Pixles conversion
        /// </summary>
        /// <param name="context"></param>
        /// <param name="valueInDp"></param>
        /// <returns></returns>
        public static float DpToPixels(Context context, float valueInDp)
        {
            DisplayMetrics metrics = context.Resources.DisplayMetrics;
            return TypedValue.ApplyDimension(ComplexUnitType.Dip, valueInDp, metrics);
        }


    }
}
[程序集:导出渲染器(typeof(CurvedCornersLabel)、typeof(CurvedCornerLabelRenderer))]
名称空间CarouselDemo.Droid
{
公共类CurvedCornerLabelRenderer:LabelRenderer
{
私有GradientDrawable_gradientBackground;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
变量视图=(CurvedCornersLabel)元素;
if(view==null)返回;
//为曲线背景创建可绘制的渐变
_gradientBackground=新的GradientDrawable();
_gradientBackground.SetShape(ShapeType.Rectangle);
_gradientBackground.SetColor(view.CurvedBackgroundColor.ToAndroid());
//笔划线的厚度
_gradientBackground.SetStroke(4,view.CurvedBackgroundColor.ToAndroid());
//曲线半径
_gradientBackground.SetCornerRadius(
DpToPixels(this.Context,
转换为单个(视图.曲线拐角半径));
//设置标签的背景
对照组:背景(_gradientBackground);
}
/// 
///与设备无关的像素到实际像素的转换
/// 
/// 
/// 
/// 
公共静态浮点DpToPixels(上下文上下文,浮点值indp)
{
DisplayMetrics=context.Resources.DisplayMetrics;
返回类型value.ApplyDimension(ComplexUnitType.Dip、valueInDp、metrics);
}
}
}
这是Xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="CarouselDemo.LabelViewDemo"
         xmlns:mr="clr-namespace:MR.Gestures;assembly=MR.Gestures"
         xmlns:local="clr-namespace:CarouselDemo;assembly=CarouselDemo"
        Padding="10,20,10,10">


<StackLayout Padding="20">
    <Label Text="Card 1"
           HeightRequest="100"
           BackgroundColor="LightGoldenrodYellow"
           x:Name="card1"
           />

    <local:CurvedCornersLabel Text="Card 2"
           HeightRequest="100"
           BackgroundColor="LightBlue"
           x:Name="card2"
           Margin="0,-40,0,0"
           CurvedCornerRadius="15"               
           ></local:CurvedCornersLabel>
</StackLayout>

</ContentPage>


有什么想法吗?

所以我找到了解决办法。首先,我将自定义渲染器中的背景色更改为红色,这是用红色显示圆角,但在覆盖中显示蓝色方形角。当我从Xaml中删除“浅蓝色”并通过自定义渲染器设置颜色时,它就工作了

这个
protected override void OnElementChanged(ElementChangedEventArgs e)
不应该是
protected override void OnElementChanged(ElementChangedEventArgs e)
吗?我不这么认为,将其更改为只会抛出一个错误,错误是?工具提示说“找不到合适的方法来覆盖”不用担心,我发现了问题。我在Xaml中设置了一种颜色,这将覆盖自定义渲染器中的颜色属性。有趣的是,在各种平台上进行了这么多年的UI开发之后,旧的“临时更改背景颜色”技巧仍然非常有用。