2016-04-29 39 views
0

我有一個Mvvmcross/Xamarin應用程序在droid部分的問題。 我已經做了「MvxSpinner菜單」至極的情侶名單上綁定 ViewModel.csXamarin.Android/MvvmCross:使用MvxSpinner導航

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" /> 

當我選擇一個項目,我設置的SelectedAction我FirstViewModel並顯示我想要加載的視圖模型。但是,當我回到FirstViewModel時,它會自動重新設置SelectedAction並返回到第二個ViewModel。回到頂端這篇文章中的信息適用於: 我嘗試在Init,ReloadState,Start,InitFromBundle和ReloadFromBundle中將我的SelectedAction設置爲none,但在所有這些調用之後,還有另一個具有我之前選擇的值,我不知道它來自哪裏。

回答

0

我提議一個聽者的SelectedItem,並設置的SelectedItem添加到0

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

感謝您的幫助,但這不起作用。而且,我們正試圖在機器人部分儘可能減少代碼。 –

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" 
+0

感謝您的幫助,但它不起作用,當我選擇一個項目時出現此警告: MvxBind:警告:62,44 SetValue在綁定中被忽略 - target屬性爲只讀 並且沒有其他事情發生。 (我修改了你的代碼:我改變了ICommand _itemSelected到MvxCommand _itemSelected) –