1
我剛剛開始嘗試使用Visual Studio的MonoDroid。MonoDroid將圖像下載到ImageView
嘗試從網站下載圖像並將其顯示在ImageView控件中。
不幸的是,一些奇怪的原因阻止圖像顯示在ImageView中。儘管圖像似乎已成功下載,但該控件仍爲空白。
任何我在這裏做錯了嗎?
的代碼如下:
private void DoClick(object sender, EventArgs e)
{
WebClient web = new WebClient();
web.DownloadDataCompleted += new DownloadDataCompletedEventHandler(web_DownloadDataCompleted);
web.DownloadDataAsync(new Uri(@"http://upload.wikimedia.org/wikipedia/commons/d/d9/Test.png"));
Toast.MakeText(this, "Image downloaded!", ToastLength.Short).Show();
}
void web_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (e.Error != null)
{
Toast.MakeText(this, e.Error.Message, ToastLength.Short).Show();
}
else
{
Bitmap bm = BitmapFactory.DecodeByteArray(e.Result, 0, e.Result.Length);
ImageView imgView = FindViewById<ImageView>(Resource.Id.MyImageView);
imgView.SetImageBitmap(bm);
}
}
隨着Main.axml定義如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Hello"
/>
<ImageView
android:id="@+id/MyImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_below="@+id/MyButton"/>
</LinearLayout>