Monodroid getApplicationContext()?

发布于 2024-12-09 13:26:50 字数 344 浏览 0 评论 0原文

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

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

编辑:我想从活动访问它

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):

Edit: I want to access it from an Activity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情魔剑神 2024-12-16 13:26:50

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

您在问题中没有提及您尝试从何处调用它。是来自活动内部吗?如果是这样,请尝试将 Application 属性强制转换为您的自定义应用程序类。类似于:

((CustomApplication) Application).DoSomething();

编辑

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

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

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

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();

Edit

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
眼眸里的那抹悲凉 2024-12-16 13:26:50

(免责声明:我是一个 monodroid 新手)

根据我所能找到的,没有静态上下文。相反,上下文是您当前活动的属性。

因此,在您当前的活动中,访问 this.ApplicationContext。

您将需要通过应用程序堆栈传递上下文;我可以预见这会变得很麻烦。

(Disclaimer: I'm a monodroid newbie)

Based on what I can find, there isn't a static context. Instead, the context is a property of your current activity.

Thus, in your current activity, access this.ApplicationContext.

You will need to pass the context via the application stack; which I can anticipate could become cumbersome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文