简体   繁体   English

Xamarin.Android MVVMCross上的MvxSpinner绑定问题

[英]MvxSpinner Binding Issue on Xamarin.Android MVVMCross

Using Xamarin.Android with MVVMCross. 将Xamarin.Android与MVVMCross结合使用。 When Select the value from MVXSpinner that Not Binding to SelectedYear Property in the Model. 从MVXSpinner中选择未绑定到模型中的SelectedYear属性的值时。 When Load the page debugger comes in to SelectedYear Property, But When I select the value from spinner it want comes into SelectedYear . 当加载页面调试器进入SelectedYear属性时,但是当我从微调器中选择值时,它想要进入SelectedYear I haven't get any errors. 我没有任何错误。 Please Any one Advice me where I am wrong. 请任何人给我建议,我错了。

Found Binding Issue on Output Window 在输出窗口上发现绑定问题

MvxBind:Warning:228.30 Failed to create target binding for binding SelectedItem for SelectedYear [0:] MvxBind:Warning:228.30 Failed to create target binding for binding SelectedItem for SelectedYear 11-09 11:50:03.756 I/mono-stdout(32419): MvxBind:Warning:228.30 Failed to create target binding for binding SelectedItem for SelectedYear MvxBind:警告:228.30无法为SelectedYear [0:]创建用于绑定SelectedItem的目标绑定。MvxBind:警告:228.30无法为SelectedYear的绑定SelectedItem创建目标绑定。11-09 11:50:03.756 I / mono-stdout(32419) :MvxBind:警告:228.30无法创建用于绑定SelectedYear的SelectedItem的目标绑定

   <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 File below. 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 file Below. 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" />

In View Model I have Following Properties. 在视图模型中,我具有以下属性。 And In the Year Model I have a Property DisplayYear . 在Year Model中,我有一个Property 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);
            }
        }

Maybe if I post something that works for me it can point you in the right direction. 也许如果我发布对我有用的东西,它可以为您指明正确的方向。

Spinner: 微调:

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

ViewModel: 视图模型:

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: 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();
    }
}

Answer here https://github.com/MvvmCross/MvvmCross-AndroidSupport/issues/80 在这里回答https://github.com/MvvmCross/MvvmCross-AndroidSupport/issues/80

You'll need to call MvxAppCompatSetupHelper.FillTargetFactories from your Setup.cs. 您需要从Setup.cs调用MvxAppCompatSetupHelper.FillTargetFactories。 Maybe there's a way we can automate that via the plugin architecture? 也许有一种方法可以通过插件架构实现自动化?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM