Bug 57137 - Navigation Renderer OnPushAsync Causes System.InvalidCast Exception for Droid
Summary: Navigation Renderer OnPushAsync Causes System.InvalidCast Exception for Droid
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.3.5
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-06-04 13:54 UTC by Bernard Blanckaert
Modified: 2017-06-17 19:35 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Screenshot showing error (211.85 KB, image/png)
2017-06-04 13:54 UTC, Bernard Blanckaert
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Bernard Blanckaert 2017-06-04 13:54:34 UTC
Created attachment 22673 [details]
Screenshot showing error

A System.InvalidCast Exception is thrown for Android when using a custom Navigation Renderer and when calling the 'base.OnPushAsync(view, false)' method.

REPLICATION CODE on GITHUB
--------------------------
To replicate, please run the following code from Github: https://github.com/bbl-Laobu/CustomBackIcon.git 


BASIC CLASSES:
--------------
The following classes are used to demonstrate the bug:

CustomNavigationPage Class
---------------------------
namespace CustomBackIcon
{
    public class CustomNavigationPage : NavigationPage
    {
        public CustomNavigationPage(Page startupPage) : base(startupPage)
        {
        }
    }
}

App Class
----------
namespace CustomBackIcon
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new CustomNavigationPage(new StartPage());
        }
...
}



NavigationPageRendererDroid Class
----------------------------------
using System.Threading.Tasks;
using CustomBackIcon;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(CustomNavigationPage), typeof(ChangeBackIcon.Droid.NavigationPageRendererDroid))]
namespace ChangeBackIcon.Droid
{
    public class NavigationPageRendererDroid : NavigationRenderer
    {
        public NavigationPageRendererDroid() : base()
        {
        }

        protected override Task<bool> OnPopViewAsync(Page page, bool animated) 
        {
            System.Diagnostics.Debug.WriteLine(">>> DEBUG Droid: OnPopViewAsync");
            return base.OnPopViewAsync(page, false);    
        }

		protected override Task<bool> OnPushAsync(Page view, bool animated)
		{  
			System.Diagnostics.Debug.WriteLine(">>> DEBUG Droid: OnPushAsync");
			return base.OnPushAsync(view, false);
		}
    }
}

---> END
Comment 1 Paul DiPietro [MSFT] 2017-06-04 21:12:25 UTC
The cause of your crash appears to be due to the fact that the app is running under AppCompat via FormsAppCompatActivity. There are two renderers for NavigationPage; NavigationRenderer, which your example uses, and NavigationPageRenderer, which is what is used by AppCompat. Adjusting your renderer to inherit from NavigationPageRenderer instead should resolve the crash. Similarly, changing the app to inherit from FormsApplicationActivity while leaving the example's renderer as is would let the app run as expected.
Comment 2 Bernard Blanckaert 2017-06-17 19:35:16 UTC
Yes indeed, the System.InvalidCast Exception gets indeed resolved this way.

Thanks!