【问题标题】:MvvmCross binding same ViewModel to both MT.Dialog RootElement (iOS) and MvxSpinner (Android)MvvmCross 将相同的 ViewModel 绑定到 MT.Dialog RootElement (iOS) 和 MvxSpinner (Android)
【发布时间】:2014-11-23 17:15:43
【问题描述】:

使用 MvvmCross,我有一个 ViewModel,其中包含我想在 iOS 和 Android 中绑定的属性。该属性表示用户从项目列表中选择的项目。

在 iOS 中,我使用 MT.Dialog RootElementRadioElements 实现了列表,绑定到 RootElementRadioSelected 属性(类似于 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


【解决方案1】:

一个简单的解决方案是使用 RaisePropertyChanged 链接这两个属性 - 例如

public class FirstViewModel : MvxViewModel
{
    private int _currentDessertIndex;
    public int CurrentDessertIndex 
    {   
        get { return _currentDessertIndex; }
        set { 
            _currentDessertIndex = value; 
            _currentDessert = _desserts[value]; 
            RaisePropertyChanged(() => CurrentDessertIndex); 
            RaisePropertyChanged(() => CurrentDessert); 
        }
    }

    private Dessert _currentDessert;
    public Dessert CurrentDessert 
    {   
        get { return _currentDessert; }
        set { 
            _currentDessert = value; 
            _currentDessertIndex = _desserts.IndexOf(_currentDessert);
            RaisePropertyChanged(() => CurrentDessertIndex); 
            RaisePropertyChanged(() => CurrentDessert); 
        }
    }
}

或者,将自定义绑定添加到 Android 以支持项目中的索引位置会相当简单。您需要将此代码基于https://github.com/MvvmCross/MvvmCross/blob/3.2/Cirrious/Cirrious.MvvmCross.Binding.Droid/Target/MvxSpinnerSelectedItemBinding.cs - 但使用整数选择位置而不是所选对象本身。请参阅http://mvvmcross.blogger.com中有关自定义绑定的 N+1

【讨论】:

  • 谢谢@Stuart。我最终为 Android 微调器创建了一个自定义绑定以使用索引。
猜你喜欢
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
相关资源
最近更新 更多