【问题标题】:Xamarin forms MVVM Stacklayout children bindingXamarin 形成 MVVM Stacklayout 子绑定
【发布时间】:2018-07-14 11:16:07
【问题描述】:

我想将 ViewModel 中的数据(子项)绑定到 StackLayout。

在 XAML 中,我想要类似:

<StackLayout Content="{Binding MainStackLayout}"/>

有没有办法制作这个?

【问题讨论】:

  • 到目前为止您尝试过什么?您目前遇到了哪些错误或问题?
  • 是的,您只需要让您的 ViewModel 具有public View MainStackLayout { get; set; } 的属性(在设置器中使用属性更改方法)。
  • 如果您将控件(您的堆栈布局内容)放在视图模型中,它确实会破坏 MVVM 模式。您应该将视图与视图模型分开。
  • 那么我应该保留stacklayout吗?
  • 我不确定你是否已经这样做了,但我强烈建议你阅读xamarin developers site article about MVVM。当你打破一种模式时,正如@KenTucker 建议的那样,你将不得不在未来遇到更大的问题,这是不值得的......

标签: c# xamarin mvvm xamarin.forms


【解决方案1】:

首先,StackLayout 没有“Content”属性。现在,如果你想在里面绑定一些内容,那就是

<StackLayout Children="{Binding MyChildren}"/>

您的任何 BindingContext(您的模型)都必须实现 INotifyPropertyChanged 接口,并且变量可能如下所示:

private List<View> _MyChildren;
public List<View> MyChildren
{
    get { return _MyChildren; }
    set
    {
        if (_MyChildren != value)
        {
            _MyChildren = value;
            OnPropertyChanged();
        }
    }
}

【讨论】:

猜你喜欢
  • 2017-05-10
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 2018-03-29
相关资源
最近更新 更多