Xamarin.Android / MvvmCross:使用MvxSpinner进行导航

时间:2016-04-29 14:30:07

标签: xamarin navigation viewmodel mvvmcross

我在机器人部分有一个Mvvmcross / Xamarin应用程序的问题。 我制作了一个" MvxSpinner菜单"这是绑在一对夫妇的名单上 ViewModel.cs

private List<CoupleIntString> _actions = new List<CoupleIntString>() {
    new CoupleIntString(0,"Actions"),
    new CoupleIntString(1,  "Mail"),
    new CoupleIntString(2,"Imprimer"),
    new CoupleIntString(3, "Totaux"),
    new CoupleIntString(4, "Fiche client")
};

public List<CoupleIntString> Actions {
    get { return _actions; }
    set {
        _actions = value;
        RaisePropertyChanged(() => Actions);
    }
}

droid.axml

<MvxSpinner
    android:id="@+id/action_spinner"
    android:layout_weight="2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    local:MvxItemTemplate="@layout/item_spinner"
    local:MvxDropDownItemTemplate="@layout/item_spinnerdropdown"
    local:MvxBind="ItemsSource Actions;SelectedItem ActionSelected" />

当我选择一个项目时,我设置了我的FirstViewModel的SelectedAction并显示了我要加载的viewmodel。

public CoupleIntString ActionSelected {
    set {
        int xx = value.intPart;
        switch (xx) {
        case 1: //mail
            GoToMailCommand.Execute();
            break;
        case 2: //impression
            GoToImpressionCommand.Execute();
            break;
        case 3: //totaux
            GoToTotauxCommand.Execute();
            break;
        case 4: //impression
            GoToDetailsClientCommand.Execute();
            break;
        default:
            break;
        }
    }

但是,当我回到FirstViewModel时,它会自动重新设置SelectedAction并返回第二个Viewmodel。 我试图在Init,ReloadState,Start,InitFromBundle和ReloadFromBundle中将我的SelectedAction设置为none,但在所有这些调用之后,还有另一个具有我之前选择的值并且我不知道它来自何处。

2 个答案:

答案 0 :(得分:0)

我建议添加一个SelectedItem侦听器并将SelectedItem设置为0。

var spinner = FindViewById<MvxSpinner>(Resource.Id.action_spinner);
spinner.ItemSelected += Spinner_ItemSelected;

答案 1 :(得分:0)

MvvmCross对SelectedItem有一个MvxSpinner目标绑定,你的代码几乎是正确的。但是,您需要使用命令而不是属性来使SelectedItem绑定起作用:

private MvxCommand<CoupleIntString> _itemSelected;
public ICommand ItemSelected =>
    _itemSelected = _itemSelected ?? 
        (_itemSelected = new MvxCommand<CoupleIntString>(DoItemSelected));

private void DoItemSelected(CoupleIntString item)
{
    ActionSelected = item;
}

然后使你的绑定看起来像:

local:MvxBind="ItemsSource Actions; SelectedItem ItemSelected"