【问题标题】:Failed to create target binding for binding SelectedItem无法为绑定 SelectedItem 创建目标绑定
【发布时间】:2016-05-27 02:48:39
【问题描述】:

我尝试将 MvxListView 的选定项绑定到我的属性。这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        layout="@layout/toolbar_actionbar" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="6dp"
        android:paddingRight="6dp">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_search_text"
            local:MvxBind="Text SearchText" />
        <MvxListView
            android:id="@+id/category_list"
            android:orientation="vertical"
            android:choiceMode="singleChoice"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            local:MvxItemTemplate="@layout/listitem_category"
            local:MvxBind="ItemsSource Categories; SelectedItem SelectedCategory" />
    </LinearLayout>
</LinearLayout>

这里的ItemSource是正确绑定的。

视图模型:

[ImplementPropertyChanged]
public class SelectCategoryListViewModel : AbstractCategoryListViewModel
{
    /// <summary>
    ///     Creates an CategoryListViewModel for the usage of providing a category selection.
    /// </summary>
    /// <param name="categoryRepository">An instance of <see cref="IRepository{T}" /> of type category.</param>
    /// <param name="dialogService">An instance of <see cref="IDialogService" /></param>
    public SelectCategoryListViewModel(IRepository<Category> categoryRepository,
        IDialogService dialogService) : base(categoryRepository, dialogService)
    {}

    public Category SelectedCategory { get; set; }

    public MvxCommand DoneCommand => new MvxCommand(Done);

    public MvxCommand CancelCommand => new MvxCommand(Cancel);

    private void Done()
    {
        MessageHub.Publish(new CategorySelectedMessage(this, SelectedCategory));
        Close(this);
    }

    private void Cancel()
    {
        Close(this);
    }
}

notify PropertyChanged 不是通过 Fody 完成的,Categories 是在父 VM 中。

public class SelectCategoryListActivity : MvxFragmentCompatActivity<SelectCategoryListViewModel>
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.fragment_category_list);

        SetSupportActionBar(FindViewById<Toolbar>(Resource.Id.toolbar));
        SupportActionBar.SetDisplayHomeAsUpEnabled(true);
    }
...

我得到的警告是:

[0:] MvxBind:Warning: 5.11 无法为 SelectedCategory 的绑定 SelectedItem 创建目标绑定

想法?

【问题讨论】:

    标签: mvvmcross


    【解决方案1】:

    我刚刚遇到SelectedItem 绑定失败的类似问题。解决的办法是将 Mvx 更新到 4.1.6 版。原因是你需要注册MvxAppCompatSetupHelper.FillTargetFactories,4.1.6现在是自动完成的。

    或者,您可以通过覆盖 FillTargetFactories 在 setup.cs 中手动注册它:

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        MvxAppCompatSetupHelper.FillTargetFactories(registry);
        base.FillTargetFactories(registry);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 2012-08-26
      • 2019-11-10
      • 2011-05-24
      • 1970-01-01
      相关资源
      最近更新 更多