Monodroid getApplicationContext()?
有谁知道如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Android 版 Mono 中,像 getApplicationContext() 这样的 Java getter/setter 方法被转换为属性:ApplicationContext
您在问题中没有提及您尝试从何处调用它。是来自活动内部吗?如果是这样,请尝试将 Application 属性强制转换为您的自定义应用程序类。类似于:
编辑
您还应该确保您的自定义应用程序类用 ApplicationAttribute:
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:
Edit
You should also make sure that your custom application class is decorated with ApplicationAttribute:
(免责声明:我是一个 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.