使用渲染器更改android中的背景图会生成null引用

时间:2018-03-05 17:48:42

标签: android xamarin xamarin.android

我想使用下一个代码

更改android中的背景图标
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Graphics.Drawables;
using Project;
using Project.Droid;
using System.ComponentModel;

[assembly: ExportRenderer(typeof(ContentPage), 
typeof(NavigationPageRendererDroid))]
namespace Project.Droid
{
public class NavigationPageRendererDroid : PageRenderer
{
    ActionBar actionBar;
    public NavigationPageRendererDroid(Context context) : base(context)
    {

    }

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

        actionBar = ((Android.App.Activity)Context).ActionBar;

        Console.WriteLine(actionBar);
        actionBar.SetCustomView(Resource.Layout.CustomNavigationBarLayout);
        actionBar.SetDisplayShowCustomEnabled(true);

        // This function is used for hide the App Icon
        actionBar.SetIcon(new ColorDrawable(Color.Transparent.ToAndroid()));

        Element.Appearing += (object sender, EventArgs ex) => {
            // Update Title and Back Icon when user go back to this page
            UpdatePageTitle();

            UpdateBackIcon();
        };
    }

    void UpdateBackIcon()
    {
        var actionBar = ((Android.App.Activity)Context).ActionBar;
        if (actionBar == null || actionBar.CustomView == null)
            return;


        // If you want to hide the back button in some pages, 
        // you can pass a value to renderer and do this.

            actionBar.SetHomeAsUpIndicator(Resource.Drawable.LeftArrow);

    }

    void UpdatePageTitle()
    {
        var actionBar = ((Android.App.Activity)Context).ActionBar;
        if (actionBar == null || actionBar.CustomView == null)
            return;

        var view = actionBar.CustomView.FindViewById<TextView>(Resource.Id.TitleText);
        if (view != null)
            view.Text = Element.Title;
    }

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        // As we used customer view, so Title cannot be update after page loaded.
        if (e.PropertyName == "Title")
            UpdatePageTitle();

    }
}

}

但是我在这行代码中得到了nullreference的错误

actionBar.SetCustomView(Resource.Layout.CustomNavigationBarLayout);

并在Resources文件夹中定义了CustomNavigationBarLayout,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
    android:id="@+id/TitleText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Title"
    android:textStyle="bold"
    android:textSize="18sp"
    android:textColor="@android:color/white" />
</RelativeLayout>

并且没有识别android:orientation,但是,如果我擦除这一行,我仍然会在actionBar.SetCustomView()中获得nullreference错误

0 个答案:

没有答案