【问题标题】:Support namespaces/references Java to C# equivalent in Android-Visual Studio在 Android-Visual Studio 中支持命名空间/引用 Java 到 C# 等价物
【发布时间】:2015-04-27 12:29:49
【问题描述】:

我正在开发一个应用程序,我将代码从 Java 转换为 C#,以在 android 上以离线模式创建自定义 MapView。

我正在使用这个用 Java 编写的 tutorial,并且有一些命名空间我无法理解本教程的创建者甚至从哪里得到它们,尤其是写为 import com.mapapp.mapapp.R; 的特定命名空间。

为了指定我在哪里工作,我正在从事这项活动。 我什至不知道在尝试查找有关它的信息时要搜索什么,因为它被写成好像它是项目中的一个类,但是通过查看package com.mapapp.main;,它似乎应该是一个 nuget 包或在同一解决方案中引用不同的项目。但是创作者在教程中从来没有提到过类似的东西。

package com.mapapp.main;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.KeyEvent;

import com.mapapp.helpers.PointD;
import com.mapapp.mapapp.R;
import com.mapapp.tileManagement.TilesProvider;
import com.mapapp.views.MapView;
import com.mapapp.views.MapViewLocationListener;

在这种情况下,这个命名空间在 C# 中的等价物是什么,什么是 R?

这是代码中使用 R 的部分:

void initViews()
    {
        // Creating the bitmap of the marker from the resources
        Bitmap marker = BitmapFactory.decodeResource(getResources(), R.drawable.marker);

        // Creating our database tilesProvider to pass it to our MapView
        String path = Environment.getExternalStorageDirectory() + "/mapapp/world.sqlitedb";
        tilesProvider = new TilesProvider(path);

        // Creating the mapView and make sure it fills the screen
        Display display = getWindowManager().getDefaultDisplay();
        mapView = new MapView(this, display.getWidth(), display.getHeight(), tilesProvider, marker);

        // If a location was saved while pausing the app then use it.
        if (savedGpsLocation != null) mapView.setGpsLocation(savedGpsLocation);

        // Update and draw the map view
        mapView.refresh();
    }

【问题讨论】:

    标签: java c# android namespaces


    【解决方案1】:

    'R' 只是一个由编译器自动生成的公共类,它包含对唯一资源 ID 的静态引用。

    当任何资源被编译时(例如图像、字符串、XML 样式等),每次编译都会给它一个唯一的整数。

    为了方便使用,R.java 包含每个资源的静态常量名称。

    请注意,不能保证两次编译之间的 ID 相同,因此生成 R.java 并且是唯一受支持的引用资源的方式。

    这是一个例子。

    public final class R {
        public static final class attr {
        }
        public static final class drawable {
            public static final int marker=0x7f020000;
        }
        public static final class id {
            public static final int b1=0x7f050001;
            public static final int text1=0x7f050000;
        }
        public static final class layout {
            public static final int main=0x7f030000;
        }
        public static final class string {
    
            public static final int app_name=0x7f040001;
            public static final int app_name1=0x7f040003;
    
            public static final int hello=0x7f040000;
            public static final int hello1=0x7f040002;
        }
    }
    

    'R.drawable.marker' 将解析为'0x7f020000'

    You can read more here.

    【讨论】:

      【解决方案2】:

      虽然接受的答案是好的并且它回答了“什么是 R?”,但它仍然是 java 并且问题明确地说是 C#。

      在最新版本的 Xamarin.Android (6.1.2.21) 中,R 的正确用法是:

      字符串

      Resource.String.My_String
      

      界面对象

      Button button = FindViewById<Button>(Resource.Id.MyButton);
      

      可绘制

      Resource.Drawable.Icon
      

      干杯。

      【讨论】:

        猜你喜欢
        • 2017-10-31
        • 1970-01-01
        • 2012-03-14
        • 1970-01-01
        • 2013-05-17
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        • 2013-02-18
        相关资源
        最近更新 更多