是否存在针对OnItemSelected事件的MvvmCross MvxSpinner的绑定?

时间:2013-07-16 13:37:31

标签: android spinner xamarin mvvmcross

我有一个MvxSpinner定义如下,当用户通过ItemSelected绑定在微调器中选择一个项目时,我试图触发一个命令:

    <Mvx.MvxSpinner
        style="@style/Spinners"
        android:id="@+id/jobStatusSpinner"
        android:prompt="@string/jobstatus_prompt"
        local:MvxBind="ItemsSource JobStatuses; SelectedItem SelectedJobStatus; ItemSelected FilterJobsByStatusCommand"
        local:MvxDropDownItemTemplate="@layout/spinner_jobstatuses"
        local:MvxItemTemplate="@layout/item_jobstatus" />

我已确认可以通过添加按钮从视图中调用FilterJobsByStatusCommand

<Button
    android:text="Test"
    android:background="@drawable/StandardButton"
    style="@style/Buttons.FullWidth"
    android:layout_centerHorizontal="true"
    local:MvxBind="Click FilterJobsByStatusCommand" />

这成功触发FilterJobsByStatusCommand,它还成功使用ViewModel的SelectedJobStatus属性来更新作业列表。 ItemSelected绑定是否应该工作还是我错过了其他一些代码或不同的绑定?

2 个答案:

答案 0 :(得分:11)

尝试绑定HandleItemSelected属性。 它尝试ICommand值。

<Mvx.MvxSpinner
        style="@style/Spinners"
        android:id="@+id/jobStatusSpinner"
        android:prompt="@string/jobstatus_prompt"
        local:MvxBind="ItemsSource JobStatuses; SelectedItem SelectedJobStatus; HandleItemSelected FilterJobsByStatusCommand"
        local:MvxDropDownItemTemplate="@layout/spinner_jobstatuses"
        local:MvxItemTemplate="@layout/item_jobstatus" />

答案 1 :(得分:2)

ItemSelectedEventHandler<ItemSelectedEventArgs>,因此如果没有自定义绑定,它就无法绑定。

要创建自定义绑定,请参阅http://slodge.blogspot.co.uk/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html

对于简单的事件到命令绑定,您可以将客户绑定基于以下内容:https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding.Droid/Target/MvxViewLongClickBinding.cs - 例如类似的东西:

public class SpinnerItemSelectedBinding : MvxAndroidTargetBinding
{
    private ICommand _command;

    protected MvxSpinner View
    {
        get { return (MvxSpinner) Target; }
    }

    public SpinnerItemSelectedBinding (MvxSpinner view)
        : base(view)
    {
        view.ItemSelected += ViewOnItemSelected;
    }

    private void ViewOnItemSelected(object sender, ItemSelectedEventArgs args)
    {
        if (_command == null)
            return;

        if (!_command.CanExecute(null))
            return;

        _command.Execute(null);
    }

    public override void SetValue(object value)
    {
        _command = value as ICommand;
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.OneWay; }
    }

    public override Type TargetType
    {
        get { return typeof (ICommand); }
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            var view = View;
            if (view != null)
            {
                view.ItemSelected -= ViewOnItemSelected;
            }
        }
        base.Dispose(isDisposing);
    }
}

您可以在设置中注册为:

        registry.RegisterCustomBindingFactory<MvxSpinner>("ItemSelected",
                                                        view =>
                                                        new SpinnerItemSelectedBinding (view));

如果有帮助,最近尝试自动绑定到https://github.com/slodge/MvvmCross/issues/313

上的TEventArgs事件有一些背景知识

如果您的应用只能处理绑定值而不是绑定事件/命令,那么SelectedItem绑定适用于我昨晚录制的N + 1视频 - http://slodge.blogspot.com/2013/07/n35-multibinding-with-tibet-n1-videos.html - 来源https://github.com/slodge/NPlus1DaysOfMvvmCross/blob/master/N-35-Tibet/BindMe.Droid/Resources/Layout/FirstView.axml#L17

SelectedItem也用于ApiExamples示例项目:https://github.com/slodge/MvvmCross-Tutorials/blob/master/ApiExamples/ApiExamples.Droid/Resources/Layout/Test_Spinner.axml