Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Custom NavigationRenderer System.InvalidCastException:

$
0
0

I'm trying to create a custom Navigation Page, in Android at the moment. I'm following instruction from this link:
https://stackoverflow.com/questions/46809733/how-to-add-a-gradient-in-xamarin-forms-toolbar-and-uinavigationbar/46816859

But when i try to run the application (currently only trying Android): i keep getting these error: System.InvalidCastException: within the renderer on base.OnElementChanged(e);

My full code for for the renderer is below:

`[assembly: ExportRenderer(typeof(CustomNavPage), typeof(CustomNavigationPageRenderer))]

namespace TestApp.Droid

{

public class CustomNavigationPageRenderer : NavigationRenderer
{

public CustomNavigationPageRenderer(Context context) : base(context)
{
 }

    protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
    {
        base.OnElementChanged(e); //Fails here...

        //run once when element is created
        if (e.OldElement != null || Element == null)
        {
            return;
        }

        var control = (CustomNavPage)this.Element;
        var context = (MainActivity)this.Context;

        context.ActionBar.SetBackgroundDrawable(new GradientDrawable(GradientDrawable.Orientation.RightLeft, new int[] { control.RightColor.ToAndroid(), control.LeftColor.ToAndroid() }));
    }
}

}`

And i call it within my Shared Code project as this:
******** MainPage = new CustomNavPage(new MainPage()) { LeftColor = Color.FromHex("#109F8D"), RightColor = Color.FromHex("#36ED81") }

I saw somewhere on this forum saying i should inherit from a PageRenderer and using ElementChangedEventArgs<Page> e, while this goes pass the error, the initial error, i get System.NullReferenceException: Object reference not set to an instance of an object. on custom.ActionBar. Which is right because this is a standard page renderer, and not a navigation page.

What am i doing wrong???


Viewing all articles
Browse latest Browse all 204402

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>