我正在学习本教程并且工作正常。 https://developer.xamarin.com/recipes/android/controls/imageview/display_an_image
但就我而言,我有 200 张图片。用户在EditText中写字,点击按钮后,分别显示图片。 如何更改以下代码中的图片源:
EditText edit = FindViewById<EditText>(Resource.Id.edtName);
button.Click += delegate
{
img.SetImageResource(Resource.Drawable.sample2);
};
其中“sample2”更改为 edit.Text。 (为用户写的文本...)
最佳答案
使用一个方法来改变drawable
public void changePhoto()
{
int MyPhoto;
if (edit.Text != string.Empty)
{
try
{
MyPhoto = (int)typeof(Resource.Drawable).GetField(edit.Text).GetValue(null);
}
catch
{
MyPhoto = Resource.Drawable.ErrorPhoto;
}
img.SetImageResource(MyPhoto);
}
}
button.Click += delegate
{
changePhoto();
};
关于c# - Xamarin - 如何动态更改图像源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43022818/