2015-11-08 61 views
1

使用Xamarin.Android與MVVMCross。當從MVXSpinner中選擇不綁定到模型中的SelectedYear屬性的值時。當加載頁面調試器進入SelectedYear屬性,但是當我從微調器中選擇它想要進入SelectedYear。我沒有得到任何錯誤。請任何人建議我在哪裏我錯了。Xamarin.Android MVVMCross上的MvxSpinner綁定問題

實測值上輸出窗口綁定問題

MvxBind:警告:228.30未能創建目標結合的SelectedItem爲SelectedYear 結合[0:] MvxBind:警告:228.30未能創建目標結合的SelectedItem爲SelectedYear 結合11月11日至9日:50:03.756 I /單標準輸出(32419):MvxBind:警告:228.30未能創建目標結合的SelectedItem爲SelectedYear

下面
<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#FFFFFF" 
     android:orientation="vertical" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="50dp"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/Retread.Collection.UI.DroidPhone.DotYear" 
      style="@style/LabelTextView" /> 
     <MvxSpinner 
      android:id="@+id/txtYear" 
      android:layout_width="20dp" 
      android:layout_height="wrap_content" 
      android:overlapAnchor="false" 
      local:MvxItemTemplate="@drawable/year_spinner" 
      local:MvxDropDownItemTemplate="@drawable/year_spinnerdropdown" 
      android:hint="@string/Retread.Collection.UI.DroidPhone.Year" 
      local:MvxBind="ItemsSource Year;SelectedItem SelectedYear" 
      style="@style/AppSpinnerStyle" /> 

Year_spinner.xml文件結合。

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:local="http://schemas.android.com/apk/res-auto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="18dip" 
     android:singleLine="true" 
     android:textColor="#000000" 
     local:MvxBind="Text DisplayYear"/> 

Year_spinnerdropdown.xml文件下面。

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    style="?android:attr/spinnerDropDownItemStyle" 
    android:singleLine="true" 
    android:background="#ffffff" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:ellipsize="marquee" 
    local:MvxBind="Text DisplayYear" /> 

在視圖模型中我有以下屬性。在年模型中,我有一個屬性DisplayYear

private List<YearModel> _Year; 
      public List<YearModel> Year 
      { 
       get 
       { 
        return _Year; 
       } 
       set 
       { 
        _Year = value; 
        RaisePropertyChanged(() => Year); 
       } 
      } 

     public YearModel SelectedYear 
     { 
      get 
      { 
       return Model.SelectedYear; 
      } 
      set 
      { 
       Model.SelectedYear = value; 
       RaisePropertyChanged(() => SelectedYear); 
      } 
     } 
+0

你並沒有清楚自己在這裏有什麼問題。預期的結果是什麼?你看到了什麼?爲什麼它錯了? – Cheesebaron

+0

預期的結果是當我發佈表單時,我需要SelectedYear屬性中的綁定值(選定的值)。但是我在SelectedYear屬性中沒有得到任何值(空)。我看到的是當我從下拉菜單中選擇一個值觸發沒有事件到ViewModel到SelectedYear屬性。我不知道我犯錯的地方。 – Dino

+0

向我們顯示您的YearModel代碼。 – PmanAce

回答

1

也許如果我張貼的東西,對我的作品能爲你指明正確的方向。

微調:

<Mvx.MvxSpinner 
    style="@style/spinner" 
    android:background="@drawable/spinner_selector" 
    local:MvxItemTemplate="@layout/item_spinner" 
    local:MvxBind="ItemsSource Position; SelectedItem SelectedPosition" 
    android:id="@+id/positionSpinner" /> 

MvxItemTemplate:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    style="@style/Spinner.MyPlayers" 
    android:singleLine="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/dummy_title" 
    local:MvxBind="Text Caption" /> 

視圖模型:

protected int SelectedPositionId 
{ 
    get { return SelectedPosition == null ? 1 : SelectedPosition.Index; } 
} 

protected SpinnerItem _selectedPosition; 
public virtual SpinnerItem SelectedPosition 
{ 
    get { return _selectedPosition; } 
    set 
    { 
     if (_selectedPosition != value) 
     { 
      _selectedPosition = value; 

      SettingsPreferences.SelectedPosition = SelectedPositionId; 

      RebuildLists(true); 

      RaisePropertyChanged(() => SelectedPosition); 
      RaisePropertyChanged(() => DisplayCleanSheets); 
      RaisePropertyChanged(() => FilteredPlayers); 
     } 
    } 
} 

List<SpinnerItem> _position; 
public List<SpinnerItem> Position 
{ 
    get 
    { 
     if (_position != null) 
      return _position; 

     _position = new List<SpinnerItem>(); 

     var values = (int[])Enum.GetValues(typeof(PositionEnumeration)); 

     foreach (var val in values.Where(p => p > 0)) 
      _position.Add(new SpinnerItem(val, SharedTextSource.GetText(Enum.GetName(typeof(PositionEnumeration), val)))); 

     return _position; 
    } 
} 

SpinnerItem:

public class SpinnerItem 
{ 
    public SpinnerItem(int index, string caption, int primaryKeyId = 0, string tag = "") 
    { 
     Index = index; 
     Caption = caption; 
     PrimaryKeyId = primaryKeyId; 
     Tag = tag; 
    } 

    public int Index { get; private set; } 

    public string Caption { get; private set; } 

    public string Tag { get; private set; } 

    public int PrimaryKeyId { get; private set; } 

    public override string ToString() 
    { 
     return Caption; 
    } 

    public override bool Equals(object obj) 
    { 
     var rhs = obj as SpinnerItem; 

     if (rhs == null) 
      return false; 

     return rhs.Caption == Caption; 
    } 

    public override int GetHashCode() 
    { 
     return Caption == null ? 0 : Caption.GetHashCode(); 
    } 
} 
1

答案在這裏 https://github.com/MvvmCross/MvvmCross-AndroidSupport/issues/80

你需要調用從 您Setup.cs MvxAppCompatSetupHelper.FillTargetFactories。也許有一種方法可以通過插件 架構實現自動化?

+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/11254333) – maazza

+1

@maazza是不是已經包含答案了? 「您需要從Setup.cs中調用MvxAppCompatSetupHelper.FillTargetFactories」 –