[英]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.我正在做一个 java 到 c# 的转换,需要帮助。 in Visual Studio 2019 Pro, Android 9.0 (Pie), I am doing this example:在 Visual Studio 2019 Pro、Android 9.0 (Pie) 中,我正在做这个例子:
https://docs.microsoft.com/en-us/xamarin/android/platform/binding-java-library/binding-a-jar 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.目标是稍后将我的 java libs 转换为 c# 以用于我的大项目。
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.我按照信中的说明进行操作,当我来到需要创建ImageView的部分时,就出现了问题。 Once I create the ImageView, it has an android:src field, something like this (auto generated):创建 ImageView 后,它有一个 android:src 字段,如下所示(自动生成):
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:""这里的问题是,当我在 VS 设计器中删除该字段时,ImageView 消失了,即使我将 srec 设置为:""
Bellow is literally all the code in MainActivity : Bellow 是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 代码:
<?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.应用程序什么都不做,在模拟器中,ImageView 是空的。 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.编辑 1:看来,问题在于 C# 代码。 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.如果我添加 android:src 字段,它仍然不会显示在模拟器上,但是当我删除代码(毕加索代码)时,它会比图像(可绘制/图标)显示的要多。 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" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.