如何从ContentView访问Xamarin Forms App对象

Geo*_* Jr 4 c# xamarin.forms

我有一个ContentView,我在我的页面上用作共享代码.它包含导航链接等.在ContentView中,我试图访问我的App类中具有以下签名的方法:

    public void ClearNavigationAndGoToPage(Page pobj_Page)
    {
        MainPage = new NavigationPage(pobj_Page);
    }  
Run Code Online (Sandbox Code Playgroud)

但是,当我在ContentView中使用以下代码行时

App.ClearNavigationAndGoToPage(new nearbyplaces());
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

非静态字段,方法或属性'App.ClearNavigationAndGoToPage(Page)'需要对象引用

我可以从ContentPages访问该方法而不是ContentViews.有什么建议?

Jas*_*son 6

使用App.Current访问您的app类的实例.在调用自定义方法之前,您需要将其强制转换为适当的类

((CustomType)App.Current).ClearNavigationAndGoToPage(new nearbyplaces());
Run Code Online (Sandbox Code Playgroud)