MvxFragment中的FindViewById

时间:2015-05-13 09:59:48

标签: c# android android-fragments mvvmcross

我需要在MvxFragment中找到我在axml中声明的视图。在活动中,我可以在OnCreate中致电MvxFragment。这不适用于片段。

这就是普通Android片段的工作原理:findViewById in Fragment

如何在不丢失绑定上下文的情况下对EnsureBindingContextIsSet()中的视图进行充气?

1 个答案:

答案 0 :(得分:5)

这是它的工作原理:

您需要了解两种扩展方法BindingInflate()public class MyFragment : MvxFragment { public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState) { this.EnsureBindingContextIsSet(savedInstanceState); var view = this.BindingInflate(Resource.Layout.YOUR_VIEW, container, false); var searchField = view.FindViewById<T>(Resource.Id.RESOURCE_ID); return view; } }

private class AlbumArtTask extends AsyncTask<String, Bitmap, Uri> {

        ImageView  mImageView;
        Context mContext;    
        public AlbumArtTask(Context context,ImageView imageView){
           this.mImageView=imageView;
           this.mContext=context;

        }

                @Override 
                protected Uri doInBackground(String... strings) {
                    final String id = strings[0];

                    // This part is what I'm asking for, basically to be able to make an Async 
                    // lookup of the url that will later be used by Picasso 
                    final Uri uri = getAlbumArtUriFromTrackId(id);

                    // Creating the Bitmap and return it to be used in an ImageView 
                    // This part is handled by Picasso 
                    return uri;
                } 

                @Override 
                protected void onPostExecute(Uri uri) {

                    // You have to pass imageview in constructor.
                    // load image into ImageView 
            Picasso.with(mContext).load(uri)/* And load it here*/.into(mImageView);
                } 
            }