【发布时间】:2016-09-25 10:45:00
【问题描述】:
基本上我想制作功能,当我向我的编辑器输入文本时,它会出现在我的标签中插入的数据。如果我将页面滑动到另一个页面,则该数据应绑定到我输入数据的上一页中的该标签。
所以我有便携式课程。在那个类中,我有方法 public ContentPage CreatePage(MyObject thing) 在这里我定义了许多标签、框、按钮等。但我会指出最重要的事情:在这里我定义我的标签和编辑器:
public partial class CoolPage: CarouselPage
{
public CoolPage()
{
foreach (MyObject p in things)
{
Children.Add(CreatePage(p));
}
}
public ContentPage CreatePage(MyObject thing) {
var emptyLabel = new Label
{
Text = "Text",
WidthRequest = 50,
HeightRequest = 50,
BackgroundColor = Color.White
};
((StackLayout)page.Content).Children.Add(emptyLabel);
var inputNumb = new Editor
{
Text=thing.Number,
TextColor = Color.Black,
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.Fill,
IsVisible = true,
BackgroundColor = Color.White
};
inputNumb.SetBinding(Label.TextProperty, "Text");
inputNumb.BindingContext = thing.Number;
((StackLayout)page.Content).Children.Add(inputNumb);
}
}
我已经尝试过执行这样的事件:
inputNumb.Completed += (sender, args) =>
{
inputNumb.SetBinding(Label.TextProperty, "Text");
inputNumb.BindingContext = thing.Number;
};
但它不起作用。我认为因为它采用相同的方法。我还尝试通过在CreatePage 方法inputCarNumb.Completed += InputCarNumb_Completed; 上实现这样的一行来超出方法范围,但是当您定义变量 inputNumb 时,它无法识别并且我不知道如何在其他情况下实现。我知道这很简单,但我想我通过 SetBinding / BindingContext 错过了一些东西。
【问题讨论】:
-
你可以试试
emptyLabel.SetBinding(Label.TextProperty, new Binding { Path = "inputNumb.Text", Mode = BindingMode.TwoWay, Source = this });或者创建一个属性,在该位置完成更改属性值并使用属性名称inputNumb.Text -
@Vishnu,谢谢你的评论。这种方法:
emptyLabel.SetBinding(Label.TextProperty, new Binding { Path = "inputNumb.Text", Mode = BindingMode.TwoWay, Source = this });不起作用。创造什么样的财产?会是setbinding属性吗? -
一个属性,例如。
InputNumbTextinCoolPage和get, set然后绑定上面的属性Path = InputNumbTextOnCompleted调用设置属性值并调用OnPropertyChanged(InputNumbText) -
@Vishnu 我试过了,但它不起作用。也许您可以将代码上传到 pastebin?
标签: c# data-binding xamarin xamarin.forms