【问题标题】:Could not implement NavigationPageRenderer Custom renderer xamarin form无法实现 NavigationPageRenderer 自定义渲染器 xamarin 表单
【发布时间】:2018-09-06 18:53:13
【问题描述】:

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

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);
        }

    }
}

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

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

【问题讨论】:

    标签: xamarin.forms custom-renderer


    【解决方案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"
    

    【讨论】:

    • 太棒了!!感谢@G.hakim 任何线索为什么我的代码不起作用?
    • 因为应用到活动的主题基本上是它的原生android东西,如果你认为这是其他人的正确答案,请标记它
    • 我其实对代码很好奇,但没关系。非常感谢
    • 这就是你需要了解的原生 android 和 appcompat 主题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多