简体   繁体   中英

How to include Java jar to C# Xamarin project (Picasso example), problem with ImageView android:src field

I am doing a java to c# transition, and need help. in Visual Studio 2019 Pro, Android 9.0 (Pie), I am doing this example:

https://docs.microsoft.com/en-us/xamarin/android/platform/binding-java-library/binding-a-jar

The goal is later to convert my java libs to c# for my big project.

I follow the instructions to the letter, and when I come to the part where I need to create an ImageView, there is the problem. Once I create the ImageView, it has an android:src field, something like this (auto generated):

android:src="@drawable/icon"

The problem here, is when I remove that field, in the VS designer, the ImageView disappears and it does not matter if I have the c# code or not, even when I set the srec to:""

Bellow is literally all the code in MainActivity :

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;

using Com.Squareup.Picasso;

namespace App3
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            /***********************************************************************/
            /**Picasso Code**/

            ImageView imageView = FindViewById<ImageView>(Resource.Id.imageView1);
            // Use the Picasso jar library to load and display this image:
            Picasso.With(this)
                .Load("http://i.imgur.com/DvpvklR.jpg")
                .Into(imageView);

            /**End Ff Picasso Code**/
            /***********************************************************************/

        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

XML Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ImageView
        android:src="@drawable/icon"             <!-- this line -->
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1" />
</LinearLayout>

The app does nothing, and in the emulator, the ImageView is empty. And there is no error, or anything.

If you need anything more, please let me know. I hope you can help me, if not, thank you for your time.

Edit 1: It seems, the problem lies in the C# code. If I add the android:src field, it still does not show up on the emulator, but when I remove the code (the Picasso code) than the image(drawable/icon) shows. Still no error!!

try this

<ImageView
        android:src="@drawable/icon"             <!-- this line -->
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minWidth="50px"
        android:minHeight="50px"
        android:id="@+id/imageView1" />

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