【发布时间】:2014-11-23 17:15:43
【问题描述】:
使用 MvvmCross,我有一个 ViewModel,其中包含我想在 iOS 和 Android 中绑定的属性。该属性表示用户从项目列表中选择的项目。
在 iOS 中,我使用 MT.Dialog RootElement 和 RadioElements 实现了列表,绑定到 RootElement 的 RadioSelected 属性(类似于 https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/DialogExamples/DialogExamples.Touch/Views/FirstView.cs 中的示例):
new Section("Radio") {
new RootElement("Dessert", new RadioGroup("Dessert", 0)) {
new Section {
radioChoices
}
}.Bind(bindings, e => e.RadioSelected, vm => vm.CurrentDessertIndex) as Element
}
其中CurrentDessertIndex 表示所选项目的索引(这是一个int)。
在 Android 中,我使用 MvxSpinner 作为列表:
<MvxSpinner
android:spinnerMode="dropdown"
local:MvxItemTemplate="@layout/itemspinner"
local:MvxDropDownItemTemplate="@layout/itemspinnerdropdown"
local:MvxBind="ItemsSource radioChoices; SelectedItem CurrentDessert" />
请注意,MvxSpinner 需要绑定到 object(而不是 int)。
这意味着我的 ViewModel 中必须有两个属性代表同一事物,具体取决于平台:
public class FirstViewModel : MvxViewModel
{
private int _currentDessertIndex;
public int CurrentDessertIndex
{
get { return _currentDessertIndex; }
set {
_currentDessertIndex = value;
RaisePropertyChanged(() => CurrentDessertIndex);
}
}
private int _currentDessert;
public int CurrentDessert
{
get { return _currentDessert; }
set {
_currentDessert = value;
RaisePropertyChanged(() => CurrentDessert);
}
}
}
如何将其合并为仅使用一个属性?
【问题讨论】:
-
赞成这个原因我也很好奇。目前我的所有解决方案都使用您描述的两个单独的属性,一个用于索引(iOS),一个用于对象(Android)。无论如何,在那一点上,我觉得合并它们以仅使用一个属性并不重要。
标签: c# xamarin mvvmcross monotouch.dialog