gpt4 book ai didi

c# - Xamarin.Android MVVMCross 上的 MvxSpinner 绑定(bind)问题

转载 作者:太空宇宙 更新时间:2023-11-03 23:26:45 24 4
gpt4 key购买 nike

将 Xamarin.Android 与 MVVMCross 结合使用。当从 MVXSpinner 中选择未绑定(bind)到模型中的 SelectedYear 属性的值时。当加载页面调试器进入 SelectedYear 属性时,但是当我从它想要的微调器中选择值时进入 SelectedYear。我没有收到任何错误。请任何人告诉我我哪里错了。

在输出窗口中发现绑定(bind)问题

MvxBind:Warning:228.30 无法为 SelectedYear 的 SelectedItem 创建目标绑定(bind)[0:] MvxBind:Warning:228.30 无法为 SelectedYear 的 SelectedItem 创建目标绑定(bind)11-09 11:50:03.756 I/mono-stdout(32419): MvxBind:Warning:228.30 无法为 SelectedYear 的 SelectedItem 创建目标绑定(bind)

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

在 View 模型中,我有以下属性。在年份模型中,我有一个属性 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);
}
}

最佳答案

也许如果我发布对我有用的东西,它可以为您指明正确的方向。

旋转器:

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

View 模型:

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;
}
}

微调项:

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

关于c# - Xamarin.Android MVVMCross 上的 MvxSpinner 绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33590442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com