如何向 Xamarin Forms 中页面标题下方的边框线添加渐变?

伊曼纽尔·特罗贝克

如何向 Xamarin Forms 中页面标题下方的边框线添加渐变?

我想要实现的一个例子是here

Cherry Bu - MSFT

如何向 Xamarin Forms 中页面标题下方的边框线添加渐变?

我创建自定义渲染来创建渐变边框。

首先,您需要定义渲染:

public class GradientViewRender : View
{
    public static readonly BindableProperty GradientColorsProperty = BindableProperty.Create<GradientViewRender, Color[]>(p => p.GradientColors, new Color[]{Color.White} );

    public Color[] GradientColors
    {
        get { return (Color[])base.GetValue(GradientColorsProperty); }
        set { base.SetValue(GradientColorsProperty, value); }
    }


    public static readonly BindableProperty ViewHeightProperty = BindableProperty.Create<GradientViewRender, double>(p => p.ViewHeight, 0);

    public double ViewHeight
    {
        get { return (double)base.GetValue(ViewHeightProperty); }
        set { base.SetValue(ViewHeightProperty, value); }
    }



    public static readonly BindableProperty LeftToRightProperty = BindableProperty.Create<GradientViewRender, bool>(p => p.LeftToRight, true);

    public bool LeftToRight
    {
        get { return (bool)base.GetValue(LeftToRightProperty); }
        set { base.SetValue(LeftToRightProperty, value); }
    }
}

此渲染采用一组颜色,这样您就可以根据渐变提供尽可能多的颜色。

然后你可以在 android 中实现这个渲染。

public class GradientViewRenderer : Xamarin.Forms.Platform.Android.ViewRenderer<GradientTest.GradientViewRender, View>
{
    LinearLayout layout;
    Xamarin.Forms.Color[] gradientColors;

    double viewHeight;


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

        if (Control == null)
        {
            layout = new LinearLayout(Application.Context);
            layout.SetBackgroundColor(Color.White);

            gradientColors = (Xamarin.Forms.Color[])e.NewElement.GradientColors;

            viewHeight = (double)e.NewElement.ViewHeight;


            CreateLayout();
        }

        if (e.OldElement != null)
        {
            // Unsubscribe from event handlers and cleanup any resources
        }

        if (e.NewElement != null)
        {
            // Configure the control and subscribe to event handlers
            gradientColors = (Xamarin.Forms.Color[])e.NewElement.GradientColors;

            viewHeight = (double)e.NewElement.ViewHeight;


            CreateLayout();
        }
    }

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == GradientViewRender.ViewHeightProperty.PropertyName)
        {
            this.viewHeight = (double)this.Element.ViewHeight;
            CreateLayout();
        }

        else if (e.PropertyName == GradientViewRender.GradientColorsProperty.PropertyName)
        {
            this.gradientColors = (Xamarin.Forms.Color[])this.Element.GradientColors;
            CreateLayout();
        }


    }

    private void CreateLayout()
    {
        layout.SetMinimumWidth((int)viewWidth);
        layout.SetMinimumHeight((int)viewHeight);

        CreateGradient();

        SetNativeControl(layout);
    }

    public void CreateGradient()
    {
        //Need to convert the colors to Android Color objects
        int[] androidColors = new int[gradientColors.Count()];

        for (int i = 0; i < gradientColors.Count(); i++)
        {
            Xamarin.Forms.Color temp = gradientColors[i];
            androidColors[i] = temp.ToAndroid();
        }

        GradientDrawable gradient = new GradientDrawable(GradientDrawable.Orientation.LeftRight, androidColors);

        if (roundCorners)
            gradient.SetCornerRadii(new float[] { cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius });

        layout.SetBackground(gradient);
    }
}

关于更详细的信息,你可以看看:

https://baglabs.com/2017/07/14/creating-gradients-xamarin-forms/

您可以从:

https://github.com/baileysh9/xamarin_forms_gradient

最后,你可以像这样得到这个渐变边框:

在此处输入图片说明

您可以通过 GradientColors 更改不同的颜色。

如果我的回复对您有帮助,请记得将我的回复标记为回复,谢谢。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Xamarin Forms中向文本单元添加图标

Xamarin Forms:如何向按钮添加填充?

如何在Xamarin.forms中向页面添加文本和图像

如何在Xamarin.Forms中向Entry添加背景图像?

Xamarin.Forms:向网格中的标签添加填充?

Xamarin Forms:如何隐藏后退按钮标题?

使用Xamarin.Forms向图像添加覆盖色

如何从Xamarin Forms ListView中删除项目?

xamarin.forms中的导航如何工作?

如何使CommandBar在Xamarin Forms UWP中呈现?

如何使用Xamarin Forms更改导航页面标题的字体?

如何在Xamarin.Forms中的网格中启用边框

如何在Xamarin.Forms中更改输入的边框颜色

Xamarin.Forms Shell如何在路线导航中向字符串注入多个和不同的值

如何在表格标题下方添加边框底部或水平线

如何从 Xamarin Forms 的页面中删除点击事件?

如何从 Xamarin.Forms 中的外部类引用页面元素?

如何在Xamarin.Forms ToolbarItem中添加菜单?

Xamarin.Forms如何在代码中添加行为

如何在 Xamarin Forms 中添加 Android .XML Vector Asset?

如何在xamarin.forms应用中添加网址

如何在Xamarin.Forms中添加图像和图标?

向Zxing条码Xaml内容页面Xamarin.forms添加元素

如何在Xamarin.Forms中切换页面?

Xamarin.Forms 如何使用 MVVMLight 切换页面

Xamarin.Forms向XAML中的所有条目添加效果

如何将Json文件添加到Xamarin Forms?

如何在Xamarin Forms中将BackgroundImage添加到ListView?

如何更改 Xamarin.Forms 中 <Label> 中字体的粗细?