Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Calling MainPage Function from Android App does not work if MainPage is NavigationPage

1mike1mike Member

Help!

In my Android Project I am trying to call a function on the MainPage like this:

(App.Current.MainPage as MainPage)?.ProcessResults(r);

The code compiles but when I execute the code, (App.Current.MainPage as MainPage) is null.

In my App.xaml.cs, my constructor code looks like this:

        public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage(new MainPage());
        }

However, if I change the following line in the constructor from:

MainPage = new NavigationPage(new MainPage());

to:
MainPage = new MainPage();

Then
(App.Current.MainPage as MainPage)?.ProcessResults(r);

Works!

How can I get (App.Current.MainPage as MainPage)?.ProcessResults(r); to work when MainPage = new NavigationPage(new MainPage());?

Thanks

Best Answer

Answers

  • 1mike1mike Member

    Both solutions you provided solved my problem. I like the MessageCenter functionality better so I changed to that. Thanks!!

Sign In or Register to comment.