首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义渲染器不呈现Xamarin窗体

自定义渲染器不呈现Xamarin窗体
EN

Stack Overflow用户
提问于 2017-06-21 08:05:20
回答 1查看 1.1K关注 0票数 1

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

下面是标签的子类:

代码语言:javascript
复制
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的渲染器:

代码语言:javascript
复制
    [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);
        }


    }
}

这是Xaml:

代码语言:javascript
复制
<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>

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-21 09:04:38

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44670245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档