2017-03-25 71 views
0

我按照这个教程,工作正常。 https://developer.xamarin.com/recipes/android/controls/imageview/display_an_imageXamarin - 如何动态更改图像源?

但在我的情况下,我有200张照片。用户将在EditText中写词,点击按钮后,分别显示图片。 我如何更改下面的代码图片来源:

EditText edit = FindViewById<EditText>(Resource.Id.edtName); 

    button.Click += delegate 
     { 
      img.SetImageResource(Resource.Drawable.sample2); 

     }; 

其中“SAMPLE2”为edit.Text变化。 (文字写用户...)

回答

1

使用的方法来改变绘制

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(); 
    };