C# Mvvmcross回收视图绑定
我试图用mvvmcross recyclerView绑定一些数据,但我面临一个问题…recyclerView总是空的。这是我的代码。我相信所有的文件都是正确的 HomeView(xml文件):C# Mvvmcross回收视图绑定,c#,xamarin,mvvmcross,C#,Xamarin,Mvvmcross,我试图用mvvmcross recyclerView绑定一些数据,但我面临一个问题…recyclerView总是空的。这是我的代码。我相信所有的文件都是正确的 HomeView(xml文件): 产品\项目\行: <!-- language: xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="h
产品\项目\行:
<!-- language: xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="@dimen/card_margin"
android:clickable="true"
android:elevation="3dp"
android:foreground="?attr/selectableItemBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="@dimen/card_cover_height"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:src="@drawable/ic_store_24"
android:scaleType="fitXY" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/thumbnail"
android:lines="2"
android:paddingLeft="@dimen/card_name_padding"
android:paddingRight="@dimen/card_name_padding"
android:paddingTop="@dimen/card_name_padding"
android:textColor="#111"
android:textSize="11dp"
android:text="Name"
app:MvxBind="Text Name"/>
<TextView
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_marginRight="10dp"
android:gravity="right"
android:paddingBottom="@dimen/card_price_padding_bottom"
android:textColor="@color/colorAccent"
android:textSize="11dp"
android:text="Price"
app:MvxBind="Text Price"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Ma视图模型(HomeViewModel):
公共类HomeViewModel:MvxViewModel
{
私人只读iCarouseService(uCarouseService);;
私有只读IPProductDataService\u productDataService;
私人MvxObservableCollection_传送带;
私有MvxObservableCollection\u产品;
公共MvxObservableCollection传送带
{
获取{return_carousels;}
设置
{
_转盘=价值;
RaisePropertyChanged(()=>转盘);
}
}
公共MvxObservableCollection产品
{
获取{return\u products;}
设置
{
_产品=价值;
RaisePropertyChanged(()=>产品);
}
}
公共HomeViewModel(iCarouseService CarouseService、IPProductDataService productDataService)
{
_转盘服务=转盘服务;
_productDataService=productDataService;
}
公共重写异步任务初始化()
{
Products=新的MvxObservableCollection(wait_productDataService.GetProducts());
Carousels=new MvxObservableCollection(wait _carouselService.GetImages());
}
}
我的片段:
<!-- language: c# -->
[MvxFragmentPresentation(typeof(MainViewModel), Resource.Id.content_frame, false)]
public class HomeView : BaseFragment<HomeViewModel>, IImageListener
{
protected override int FragmentId => Resource.Layout.HomeView;
CarouselView _carouselView;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.HomeView, container, false);
//var view = this.BindingInflate(Resource.Layout.HomeView, container, false);
_carouselView = view.FindViewById<CarouselView>(Resource.Id.carouselView);
var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.product_recycler);
_carouselView.PageCount = ViewModel.Carousels.Count;
_carouselView.SetImageListener(this);
if (recyclerView != null)
{
recyclerView.HasFixedSize = true;
var layoutManager = new GridLayoutManager(Activity, 2, LinearLayoutManager.Vertical, false);
recyclerView.SetLayoutManager(layoutManager);
}
return view;
}
public void SetImageForPosition(int position, ImageView imageView)
{
Picasso.With(Context).Load(ViewModel.Carousels[position].Image)
//.Placeholder(Resource.Mipmap.ic_launcher)
//.Error(Resource.Mipmap.ic_launcher)
//.Resize(500, 300)
.Fit()
.Into(imageView);
}
}
[MvxFragmentPresentation(typeof(MainViewModel),Resource.Id.content\u frame,false)]
公共类HomeView:BaseFragment,IImageListener
{
受保护的覆盖int FragmentId=>Resource.Layout.HomeView;
旋转视图(CarouselView);;
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
var视图=充气机。充气(Resource.Layout.HomeView,container,false);
//var view=this.BindingInflate(Resource.Layout.HomeView,container,false);
_carouselView=view.findviewbyd(Resource.Id.carouselView);
var recyclerView=view.findviewbyd(Resource.Id.product\u recycler);
_carouselView.PageCount=ViewModel.Carousels.Count;
_carouselView.SetImageListener(这个);
如果(recyclerView!=null)
{
recyclerView.HasFixedSize=true;
var layoutManager=new GridLayoutManager(活动,2,LinearLayoutManager.Vertical,false);
recyclerView.SetLayoutManager(layoutManager);
}
返回视图;
}
public void SetImageForPosition(整数位置,图像视图)
{
毕加索.With(Context).Load(ViewModel.Carousels[position].Image)
//.Placeholder(Resource.Mipmap.ic_启动器)
//.Error(Resource.Mipmap.ic_启动器)
//.调整大小(500300)
.Fit()
.进入(图像视图);
}
}
我错过什么了吗?非常感谢您的帮助。在您的HomeView片段中,以下一行被注释掉:
var view = this.BindingInflate(Resource.Layout.HomeView, container, false);
而且它似乎使用的是LayoutFlater提供的系统
为了让MvvmCross识别您在axml布局文件中定义的绑定,您需要通过this.BindingInflate
扩展方法使用MVXLAyontFlater
尝试修改代码,如下所示:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.HomeView, null);
_carouselView = view.FindViewById<CarouselView>(Resource.Id.carouselView);
var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.product_recycler);
_carouselView.PageCount = ViewModel.Carousels.Count;
_carouselView.SetImageListener(this);
if (recyclerView != null)
{
recyclerView.HasFixedSize = true;
var layoutManager = new GridLayoutManager(Activity, 2, LinearLayoutManager.Vertical, false);
recyclerView.SetLayoutManager(layoutManager);
}
return view;
}
public override视图OnCreateView(布局、充气机、视图组容器、Bundle savedInstanceState)
{
base.OnCreateView(充气机、容器、保存状态);
var view=this.BindingInflate(Resource.Layout.HomeView,null);
_carouselView=view.findviewbyd(Resource.Id.carouselView);
var recyclerView=view.findviewbyd(Resource.Id.product\u recycler);
_carouselView.PageCount=ViewModel.Carousels.Count;
_carouselView.SetImageListener(这个);
如果(recyclerView!=null)
{
recyclerView.HasFixedSize=true;
var layoutManager=new GridLayoutManager(活动,2,LinearLayoutManager.Vertical,false);
recyclerView.SetLayoutManager(layoutManager);
}
返回视图;
}
更多背景:
var view = this.BindingInflate(Resource.Layout.HomeView, container, false);
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.HomeView, null);
_carouselView = view.FindViewById<CarouselView>(Resource.Id.carouselView);
var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.product_recycler);
_carouselView.PageCount = ViewModel.Carousels.Count;
_carouselView.SetImageListener(this);
if (recyclerView != null)
{
recyclerView.HasFixedSize = true;
var layoutManager = new GridLayoutManager(Activity, 2, LinearLayoutManager.Vertical, false);
recyclerView.SetLayoutManager(layoutManager);
}
return view;
}