【问题标题】:How to access the Xamarin Forms App object from a ContentView如何从 ContentView 访问 Xamarin Forms App 对象
【发布时间】:2016-12-17 06:10:04
【问题描述】:

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

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

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

App.ClearNavigationAndGoToPage(new nearbyplaces());

我收到以下错误消息:

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

我可以从 ContentPages 访问该方法,但不能从 ContentViews 访问。有什么建议吗?

【问题讨论】:

  • 您尝试从静态调用的方法是什么?
  • 我想感谢 Esteban,因为我没有将方法标记为静态,因此出现问题。

标签: c# xamarin.forms


【解决方案1】:

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

((CustomType)App.Current).ClearNavigationAndGoToPage(new nearbyplaces());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多