Caliburn Micro在Xamarin Forms中的绑定代码后端

3
我正在使用Caliburn.Micro.Xamarin.Forms MVVM框架在我的PCL Xamarin Forms项目中。 我不知道如何在我的代码后台xaml.cs文件中绑定视图模型。
例如: 在XAML中,应该是这样的:
<Label Text={Binding Username}/>

但我需要在后端代码中编写这个:
Label= new Label{
 Text= ....?
};

“有人可以帮助我吗?”
1个回答

3
尽管这些东西在谷歌上很容易找到,但我会为您概述一下。
首先创建“Label”,然后绑定“Text”属性。使用以下代码:
var label = new Label(); label.SetBinding(Label.TextProperty, new Binding("PropertyOnYourViewModel"));
如果您有C# 6功能,则可以像这样消除不必要的魔术字符串:
var label = new Label(); label.SetBinding(Label.TextProperty, new Binding(nameof(PropertyOnYourViewModel)));

1
它可以工作,但是我在绑定列表方面遇到了问题,因为我无法迭代列表项,我该怎么办?我的代码:var list=new ListView(); list.setBinding(ListView.ItemsSource , new Binding("Houses")); foreach (var house in list){} 在我的ViewModel中,Houses不为空,但我无法迭代。 - Marco24690
1
如果您设置了“ItemsSource”,则无需遍历它。在您的“ListView”上设置一个“ItemTemplate”。 - Gerald Versluis
我需要做类似这样的事情:foreach (var house in houses) { cardstack.Children.Add(new CardView(house)); } Cardstack 是一个 StackLayout,而 CardView 是一个 ContentView。所以我将我的房屋绑定到 ListView 中,以便可以对它们进行迭代,但我知道这不是一个好的解决方案,而且它也不起作用。我对如何做感到困惑。 - Marco24690

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接