简体   繁体   中英

Support namespaces/references Java to C# equivalent in Android-Visual Studio

I am working on a application where I am translating code from Java to C# to create a custom MapView in offline mode on android.

I am using this tutorial that is written in Java and there are a few namespaces that I am unable to comprehend where the creator of this tutorial even got them from, especially one specific that is written as import com.mapapp.mapapp.R; .

To specify where I am at I am working on the activity. I don't even know what to search for when trying to find information on it as it is written as if it is a class in the project but by looking at the package com.mapapp.main; it seems as if it should be a nuget package or a reference to a different project in the same solution. But the creator has never mentioned anything like that in the tutorial.

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;

What in this case would be the equivalent in C# for this namespace, and what is R?

Here is the part in the code that uses 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' is just a public class automatically generated by the compiler which contains static references to unique resource IDs.

When any resource is compiled (eg image, string, XML style etc) it is given a unique integer per-compile.

To make it easy to use, R.java contains static constant names for each resource.

Note that there is no guarantee that the ID will be the same between two compiles, hence why R.java is generated and is the only supported way to reference a resource.

Here's an example.

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' will resolve to '0x7f020000'

You can read more here.

while the accepted answer is ok and it answers 'What is R?', it is still java and the question explicitly says C#.

In the latest version of Xamarin.Android (6.1.2.21), the correct usage of R would be:

String

Resource.String.My_String

UI Object

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

Drawable

Resource.Drawable.Icon

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM