1

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

    <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也成功使用SelectedJobStatusViewModel 的属性来更新作业列表。绑定应该 ItemSelected工作还是我错过了一些其他代码或不同的绑定?

4

2 回答 2

12

尝试绑定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" />
于 2014-04-01T13:29:31.713 回答
2

ItemSelected是 aEventHandler<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

于 2013-07-16T14:10:34.967 回答