如何从价值Resource.Id? [英] How to get the Resource.Id from Value?

查看:142
本文介绍了如何从价值Resource.Id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的绘制对象,MDPI文件夹,名为(my_image.png)一个图像。

I have a image called(my_image.png) in my Drawable-mdpi folder.

我的Andr​​oid应用程序与Web服务交互。它可能会发送回my_image。我的Andr​​oid应用程序应该载入了这个形象。

my android application interacts with a webservice. It might send back "my_image". My android application should load up this image.

我使用Monodroid,我试过这种

I am using Monodroid and I tried this

   int abc = Resources.GetIdentifier("my_image","drawable",null);

然而,结果总是0。当它应该是(从资源文件)

Yet the result is always "0". When it should be(from the resource file)

        // aapt resource value: 0x7f020000
        public const int my_image = 2130837504;

环顾四周,Android的方式似乎是类似

Looking around and the android way seems to be similar

int i = this.getResources().getIdentifier("txt_asecondtext", "strings", this.getPackageName());

我试图通过在包名称,而不是,但是这并没有什么。

I tried passing in the package name instead of null but that did nothing.

推荐答案

现在的问题是双重的:

  1. 您需要提供在 Resources.GetIdentifier()呼叫包名称,而不是使用
  2. 您需要用正确的软件包名称。
  1. You need to provide a package name in the Resources.GetIdentifier() call, and not use null.
  2. You need to use the correct package name.

最简单的方法,以确保您获得正确的包名是使用<一个href="http://docs.mono-android.net/index.aspx?link=P%3aAndroid.Content.Context.PackageName">Android.Content.Context.PackageName属性:

The simple way to ensure you get the correct package name is to use the Android.Content.Context.PackageName property:

int id = Resources.GetIdentifier ("my_image", "drawable", PackageName);

如果你不想/不能使用 Context.PackageName ,然后看生成输出,在如在 OBJ \调试\机器人\的Andr​​oidManifest.xml 文件,并使用的 /清单/ @包属性的值值。例如,的Andr​​oidManifest.xml 可能包含:

If you don't want to/can't use Context.PackageName, then look at the build output, in e.g. the obj\Debug\android\AndroidManifest.xml file, and use the value of the /manifest/@package attribute value. For example, AndroidManifest.xml may contain:

<?xml version="1.0" encoding="utf-8"?>
<manifest
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:versionCode="1"
        android:versionName="1.0"
        package="MonoAndroidApplication2.MonoAndroidApplication2">
    <!-- ... -->
</manifest>

因此​​,你也可以使用:

Thus, you could instead use:

int id = Resources.GetIdentifier ("my_image", "drawable", 
        "MonoAndroidApplication2.MonoAndroidApplication2");

有关 monodroid@lists.ximian.com 的用户,看到的如何使用Resources.GetIdentifier()线程,和的后续消息

For monodroid@lists.ximian.com subscribers, see the How to use Resources.GetIdentifier() thread, and the followup message.

这篇关于如何从价值Resource.Id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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