创建自定义Spinner

时间:2014-03-10 22:10:32

标签: xamarin.android mvvmcross

我正在尝试自定义MvxSpinner以添加一些额外的控件,这是我的代码:

public class ChamSpinner : LinearLayout
{
    public Spinner Spinner{ get; private set; }

    public EventHandler<AdapterView.ItemSelectedEventArgs> ItemSelected;

    public ChamSpinner (Context context, IAttributeSet attrs) : this (context, attrs, new ChamSpinnerAdapter (context))
    {

    }

    public ChamSpinner (Context context, IAttributeSet attrs, IMvxAdapter adapter) : base (context, attrs)
    {
        ((Activity)Context).LayoutInflater.Inflate (Resource.Layout.ChamSpinnerLayout, this);
        Spinner = FindViewById<Spinner> (Resource.Id.ChamSpinnerSpinner);
        int itemTemplateId = MvxAttributeHelpers.ReadListItemTemplateId (context, attrs);
        int dropDownItemTemplateId = MvxAttributeHelpers.ReadDropDownListItemTemplateId (context, attrs);
        adapter.ItemTemplateId = itemTemplateId;
        adapter.DropDownItemTemplateId = dropDownItemTemplateId;
        Adapter = adapter;
        SetupHandleItemSelected ();
    }

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

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

            Spinner.Adapter = value;
        }
    }

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

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

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

    public ICommand HandleItemSelected { get; set; }

    private void SetupHandleItemSelected ()
    {
        Spinner.ItemSelected += (sender, args) =>
        {
            var position = args.Position;
            HandleSelected (position);
            if (ItemSelected != null)
                ItemSelected (sender, args);
        };
    }

    protected virtual void HandleSelected (int position)
    {
        var item = Adapter.GetRawItem (position);
        if (this.HandleItemSelected == null
            || item == null
            || !this.HandleItemSelected.CanExecute (item))
            return;

        this.HandleItemSelected.Execute (item);

    }
}

我正在使用它:

<cross.android.ChamSpinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        local:MvxDropDownItemTemplate="@layout/myspinneritemdropdown"
        local:MvxItemTemplate="@layout/myspinneritem"
        local:MvxBind="ItemsSource MyItemsSource; SelectedItem MyItem; Mode TwoWay" />

微调器总是空的,我试图在ItemsSource属性上添加自定义绑定,但结果仍然相同。 如何在我的微调器中显示我的物品?

提前谢谢。

2 个答案:

答案 0 :(得分:1)

我认为使用BindingInflate代替Inflate应该修复它,甚至指向正确的方向。 https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Droid/BindingContext/IMvxAndroidBindingContext.cs

((IMvxBindingContextOwner)Context).BindingInflate(Resource.Layout.ChamSpinnerLayout, this);

答案 1 :(得分:0)

我在日志中发现了这个错误

MvxBind:错误:32,12未找到查看类型 - cross.android.ChamSpinner

我的自定义控件在另一个程序集中,所以我将它添加到MvvmCross View程序集是我的Setup类这样

protected override IList<Assembly> AndroidViewAssemblies
    {
        get
        {
            var assemblies = base.AndroidViewAssemblies;
            assemblies.Add(typeof(ChamSpinner).Assembly);
            return assemblies;
        }
    }

感谢Stuart的建议以及您的精彩框架。