【发布时间】:2016-10-02 21:15:21
【问题描述】:
我在片段中使用了 MvxRecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="My Headline" />
<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
android:id="@+id/RecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Headline"
android:layout_marginTop="50dp" />
</RelativeLayout>
这是片段代码:
[MvxFragment(typeof(MainViewModel), Resource.Id.Main_ViewPager)]
[Register("my.app.fragments.MyFragment")]
public class MyFragment : BaseFragment<MyViewModel>
{
private MvxRecyclerView recyclerView;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.MyFragment, null);
this.recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.RecyclerView);
this.recyclerView.ItemTemplateId = Resource.Layout.MyCell;
var set = this.CreateBindingSet<MyFragment, MyViewModel>();
set.Bind(this.recyclerView).For(x => x.ItemsSource).To(x => x.Items);
set.Apply();
return view;
}
}
还有 ViewModel:
public class MyViewModel : MvxViewModel
{
....
private ObservableCollection<T> items;
public ObservableCollection<T> Items
{
get { return this.items; }
set
{
this.items = value;
this.RaisePropertyChanged(() => this.Items);
}
}
....
}
问题是,MvxRecyclerView 不显示任何数据。 但确实如此,当我在 axml 中定义 ItemTemplateId 和 Binding 时(例如 local:MvxItemTemplate="...", local:MvxBind="ItemsSource Items")。
我的目标不是在 axml 中定义它。我想利用在代码中使用这些属性的优势(例如重命名或重构问题)。
是否可以在代码中定义这些绑定?
【问题讨论】:
-
你能分享一下 ViewModel 吗?您的 Fragment 中的代码似乎是正确的。也许你没有使用 ObservableCollection
? -
我编辑了问题并添加了 MyViewModel 的有趣部分。它是一个 ObservableCollection
- iOS 上的绑定非常适合这个 ViewModel。 -
输出中是否有任何警告?
-
不,不属于 Mvx
-
嗯也许可以这样尝试:this.CreateBinding(recyclerview).For("ItemsSource").To
(vm => vm.Items).Apply();
标签: android xamarin binding mvvmcross