Monodroid getApplicationContext()? [英] Monodroid getApplicationContext()?

查看:34
本文介绍了Monodroid getApplicationContext()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何在 monodroid 中获取应用程序上下文(我正在寻找 getApplicationContext() 的 monodroid 实现)?我试过 Application.ApplicationContext 但它不起作用.我写了一个继承 Application 的类,我想将 ApplicationContext 显式转换为这种类型.在这一点上,mono 死了,输出上有一条非常无用的消息:

Does anybody know how to get the application context in monodroid (I'm searching for the monodroid implementation of getApplicationContext())? I tried Application.ApplicationContext but it doesn't work. I wrote a class inheriting Application and I want to explicitly cast the ApplicationContext to this type. At this point mono dies with a really unhelpful message on the output:

10-12 11:58:31.045 I/mono    ( 1919): Stacktrace:
10-12 11:58:31.045 I/mono    ( 1919):

我想从活动访问它

推荐答案

在 Mono for Android 中,像 getApplicationContext() 这样的 Java getter/setter 方法被转换为属性:ApplicationContext

In Mono for Android, Java getter/setter methods like getApplicationContext() get translated into properties: ApplicationContext

您没有在问题中提到您试图从哪里调用它.是否来自活动内部?如果是这样,请尝试将 Application 属性转换为您的自定义应用程序类.类似的东西:

You don't mention in the question where you're trying to call this from. Is it from inside an activity? If so, try casting the Application property to your custom application class. Something like:

((CustomApplication) Application).DoSomething();

编辑

您还应该确保您的自定义应用程序类使用 ApplicationAttribute:

You should also make sure that your custom application class is decorated with ApplicationAttribute:

[Application(Theme = "@style/ApplicationTheme", Label = "@string/ApplicationName", Icon = "@drawable/icon")]
public class CustomApplication : Application

这篇关于Monodroid getApplicationContext()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆