class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Method for getting bitmap is removed for code clearness
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
另外,这个例子是用 Java 编写的,但是 C # 开发人员可以理解。
来源: < a href = “ http://developersdev.blogspot.ru/2014/01/owreference-example.html”rel = “ nofollow norefrer”> http://developersdev.blogspot.ru/2014/01/weakreference-example.html