首页 文章

无法实现NavigationPageRenderer自定义渲染器xamarin表单

提问于
浏览
0

我有很多关于构建CustomRenderer以在xamarin表单上显示自定义导航页面(在导航栏上显示渐变)的google,但没有成功 . 这是我的代码:

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace BillerApp
{
    public class GradientNavigationBar: NavigationPage
    {
        public GradientNavigationBar(Page page): base(page)
        {

        }
        public GradientNavigationBar() : base()
        {

        }

        public static readonly BindableProperty StartColorProperty = BindableProperty.Create(
            nameof(StartColor),
            typeof(Color),
            typeof(GradientNavigationBar),
            Color.Default);

        public static readonly BindableProperty EndColorProperty = BindableProperty.Create(
            nameof(EndColor),
            typeof(Color),
            typeof(GradientNavigationBar),
            Color.Default);

        public Color StartColor {
            get { return (Color)GetValue(StartColorProperty); }
            set { SetValue(StartColorProperty, value); }
        }

        public Color EndColor
        {
            get { return (Color)GetValue(EndColorProperty); }
            set { SetValue(EndColorProperty, value); }
        }


    }
}

以下是Xamarin.Droid上的渲染器

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using BillerApp;
using Xamarin.Forms.Platform.Android;
using Android.Graphics.Drawables;
using BillerApp.Droid;
using System.ComponentModel;

[assembly: ExportRenderer(typeof(GradientNavigationBar), typeof(GrandientNavigationBarRenderer))]
namespace BillerApp.Droid
{    
    [Activity(Name = "com.companyname.BillerApp.MainActivity")]
    public class GrandientNavigationBarRenderer: Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer
    {

        //public GrandientNavigationBarRenderer(Context context): base(context){} //can not use this constructor

        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged(e);       // I got Invalid Cast Exception if Inherit from NavigationRenderer

            if (e.OldElement != null || Element == null)
            {
                return;
            }

            var p = this.Element as GradientNavigationBar;
            var context = (Activity)this.Context;


            var sc = new int[] { p.StartColor.ToAndroid(), p.EndColor.ToAndroid() };
            var grad = new GradientDrawable(GradientDrawable.Orientation.TopBottom, sc);

            var t = context.ActionBar;   // here i got null       
            t.SetSplitBackgroundDrawable(grad);

            context.ActionBar.SetBackgroundDrawable(grad);
        }

    }
}

这就是我想要实现的目标:
navbar

我正在使用VS 2015社区版,并且已经更新了SDK工具

1 回答

  • 1

    我只是通过添加这样的东西到你的布局文件夹来做到这一点:

    toolbar_gradient.xml

    <gradient
    android:type="linear"
    android:startColor="#F3A183"
    android:endColor="#EC6F66"
    android:angle="270"/>
    </shape>
    

    然后在Toolbar.xml中添加以下代码行:

    android:background="@layout/toolbar_gradient"
    

相关问题