2

我无法将 Cheesebarons 水平列表视图更新到新的 v3 MvvmCross。一切似乎都正常工作,除了在我的“BindableHorizo​​ntalListView”控件的构造函数中,适配器的 ItemsSource 为空。这很奇怪,因为上下文显示我试图绑定的视图模型属性非常清楚地显示有 3 个项目,并且绑定看起来非常简单。我错过了什么?我希望我已经包含了足够多的代码。我还尝试通过“OnViewModelSet”事件上的流利绑定来绑定它,结果相同。

出现警告

[MvxBind]  24.87 Unable to bind: source property source not found Cirrious.MvvmCross.Binding.Parse.PropertyPath.PropertyTokens.MvxPropertyNamePropertyToken on DeviceViewModel

AXML

 <BindableHorizontalListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        local:MvxBind="ItemsSource DevicesList; ItemClick ItemSelected"
        local:MvxItemTemplate="@layout/devices_horizontal_list_item" />

BindableHorizo​​ntalListView 控件

using System.Collections;
using System.Windows.Input;
using Android.Content;
using Android.Util;
using Cirrious.MvvmCross.Binding.Attributes;
using Cirrious.MvvmCross.Binding.Droid.Views;

namespace Project.Droid.Controls
{
    public class BindableHorizontalListView
        : HorizontalListView //Which inherits from AdapterView<BaseAdapter>
    {
        public BindableHorizontalListView(Context context, IAttributeSet attrs) 
            : this(context, attrs, new MvxAdapter(context))
        {
        }

        public BindableHorizontalListView(Context context, IAttributeSet attrs, MvxAdapter adapter)
            : base(context, attrs)
        {
            InitView ();
            var itemTemplateId = MvxAttributeHelpers.ReadListItemTemplateId (context, attrs);

            adapter.ItemTemplateId = itemTemplateId;
            Adapter = adapter;
            SetupItemClickListener();

        }

        public new MvxAdapter Adapter
        {
            get { return base.Adapter as MvxAdapter; }
            set
            {
                var existing = Adapter;
                if (existing == value)
                    return;

                if (existing != null && value != null)
                {
                    value.ItemsSource = existing.ItemsSource;
                    value.ItemTemplateId = existing.ItemTemplateId;
                }

                base.Adapter = value;
            }
        }

        [MvxSetToNullAfterBinding]
        public IEnumerable ItemsSource
        {
            get { return Adapter.ItemsSource; }
            set { Adapter.ItemsSource = value; this.Reset (); }
        }

        public int ItemTemplateId
        {
            get { return Adapter.ItemTemplateId; }
            set { Adapter.ItemTemplateId = value; }
        }

        public new ICommand ItemClick { get; set; }

        private void SetupItemClickListener()
        {
            base.ItemClick += (sender, args) =>
            {
                if (null == ItemClick)
                    return;
                var item = Adapter.GetItem(args.Position) as Java.Lang.Object;
                if (item == null)
                    return;

                if (item == null)
                    return;

                if (!ItemClick.CanExecute(item))
                    return;

                ItemClick.Execute(item);
            };
        }
    }
}

看法

[Activity (Label = "Device", ScreenOrientation = ScreenOrientation.Portrait)]
public class DeviceView : MvxActivity
{

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView(Resource.Layout.device);
    }

}

ViewModel 上的属性

private Services.Device[] _devicesList;
public Services.Device[] DevicesList {
    get {
        return _devicesList;
    }
    set {
        _devicesList = value;
        RaisePropertyChanged(() => DevicesList);
    }
}

如果 XAM STUDIO 中只有 PCL 支持,我会介入,看看其他控件是如何做到的!!!!

4

1 回答 1

2

ItemsSource 在构造函数中将始终为空 - 它是通过绑定设置的属性,并且该属性只能在构造函数完成后设置。

消息:

[MvxBind]  24.87 Unable to bind: source property source not found Cirrious.MvvmCross.Binding.Parse.PropertyPath.PropertyTokens.MvxPropertyNamePropertyToken on DeviceViewModel

包含一个错误-在最近的提交中修复-因此该消息将来应该更具可读性。

如果该错误不存在,我怀疑该消息会说问题出在DevicesList- 绑定找不到该属性。它在那里吗?它有一个get吗?是public吗?

于 2013-09-01T20:22:41.257 回答