我正在开发一个应用程序,将代码从 Java 转换为 C#,以便在 Android 上以离线模式创建自定义 MapView。
我正在使用这个tutorial这是用 Java 编写的,有一些命名空间我无法理解本教程的创建者从哪里获取它们,尤其是一个特定的命名空间,它被写为 import com.mapapp.mapapp.R;
.
指定我正在进行 Activity 的位置。
当试图查找有关它的信息时,我什至不知道要搜索什么,因为它的编写方式就好像它是项目中的一个类,但通过查看 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();
}
最佳答案
“R”只是编译器自动生成的公共(public)类,其中包含对唯一资源 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”
关于java - Android-Visual Studio 中支持 Java 到 C# 的等效命名空间/引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29895824/