Custom NavigationRenderer System.InvalidCastException:

MichaelKayMichaelKay ✭✭GBMember ✭✭
edited August 2018 in Xamarin.Forms

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: <Timeout exceeded getting exception details> 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???

Best Answer

  • MichaelKayMichaelKay ✭✭ GB ✭✭
    edited August 2018 Accepted Answer

    @BillyLiu thanks for your reply, i was planning to come back here to report my findings. The error i was getting is System.InvalidCastException: Specified cast is not valid. I guess i could try and get the full stack trace

    I don't think you can make Navigation Page Dependency Service works with current Xamarin Cross platform project version. Perhaps it's just me, but i couldn't find any way to make it work. I tried all previous old examples but none of them work, i just kept getting the same error.

    But there is far better simpler solution for what i wanted, which is to have my image button of logo in the middle of the Navigation bar. I did this by updating Toolbar.axml which is in Resources/layout of the Android project.

    So my updated Toolbar.axml looks like this:

    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/gradient"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    
    
    
    
     ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content"
      android:layout_height="wrap_content" android:adjustViewBounds="true"
      android:maxWidth="150dp"
       android:maxHeight="90dp"  android:scaleType="fitCenter" android:src="@drawable/logo" 
    

    I removed all the angle tags because the code refuse to show when i had it well formed. But above give the gist of what i did.

    If anyone else have use the custom navigation page renderer successfully in a recent Xamarin project then please let the community know how you set this up. Until then i will opt for this simpler approach.

Answers

  • BillyLiuBillyLiu Xamurai Member, Xamarin Team Xamurai

    @MichaelKay

    System.InvalidCastException: <Timeout exceeded getting exception details>

    Please run your project again to get the full error message.

    And have you tried to use Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer instead?

  • MichaelKayMichaelKay ✭✭ GBMember ✭✭
    edited August 2018 Accepted Answer

    @BillyLiu thanks for your reply, i was planning to come back here to report my findings. The error i was getting is System.InvalidCastException: Specified cast is not valid. I guess i could try and get the full stack trace

    I don't think you can make Navigation Page Dependency Service works with current Xamarin Cross platform project version. Perhaps it's just me, but i couldn't find any way to make it work. I tried all previous old examples but none of them work, i just kept getting the same error.

    But there is far better simpler solution for what i wanted, which is to have my image button of logo in the middle of the Navigation bar. I did this by updating Toolbar.axml which is in Resources/layout of the Android project.

    So my updated Toolbar.axml looks like this:

    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/gradient"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    
    
    
    
     ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content"
      android:layout_height="wrap_content" android:adjustViewBounds="true"
      android:maxWidth="150dp"
       android:maxHeight="90dp"  android:scaleType="fitCenter" android:src="@drawable/logo" 
    

    I removed all the angle tags because the code refuse to show when i had it well formed. But above give the gist of what i did.

    If anyone else have use the custom navigation page renderer successfully in a recent Xamarin project then please let the community know how you set this up. Until then i will opt for this simpler approach.

  • BillyLiuBillyLiu Xamurai Member, Xamarin Team Xamurai

    @MichaelKay

    I'm glad you have a found a solution. Please mark your solution as the answer so that can help other community members.

  • rizkidwiprizkidwip ✭✭ Member ✭✭

    What @BillyLiu said about changing to use
    Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer instead
    works for me

  • rpdomingo_18rpdomingo_18 ✭✭ Member ✭✭

    @MichaelKay Do you also experience this error?

  • MichaelKayMichaelKay ✭✭ GBMember ✭✭

    @rpdomingo_18 Yes i did.

Sign In or Register to comment.