Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the Xamarin Forms App object from a ContentView

I have a ContentView that I use as shared code on my pages. It contains navigation links etc. In the ContentView, I am trying to access a method in my App class that has the following signature:

    public void ClearNavigationAndGoToPage(Page pobj_Page)
    {
        MainPage = new NavigationPage(pobj_Page);
    }  

However, when I use the following line of code in a ContentView

App.ClearNavigationAndGoToPage(new nearbyplaces());

I get the following error message:

An object reference is required for the non-static field, method, or property 'App.ClearNavigationAndGoToPage(Page)'

I can access the method from ContentPages just not ContentViews. Any suggestions?

like image 277
George M Ceaser Jr Avatar asked Dec 16 '16 20:12

George M Ceaser Jr


1 Answers

use App.Current to access the instance of your app class. You will need to cast it to the appropriate class before calling the custom method

((CustomType)App.Current).ClearNavigationAndGoToPage(new nearbyplaces());
like image 154
Jason Avatar answered Oct 21 '22 14:10

Jason