简单的Xamarin表示代码中的文本绑定不起作用

时间:2017-03-23 02:02:23

标签: xaml xamarin

我有一个非常简单的代码示例来尝试测试数据绑定的一个方面。但是,我无法让它发挥作用,我希望有人可以向我指出我做错了什么。

在cs代码隐藏文件中,我有:

string textSample = "This is a test!";
BindingContext = this;

在xaml文件中:

<Label Text = "{Binding Path=textSample}" />

当我运行代码时,标签不会显示任何内容。

我显然错过了什么,但我看不清楚。

提前感谢您提供任何帮助

2 个答案:

答案 0 :(得分:1)

您只能绑定到公开properties

public string textSample { get { return "This is a test!"; }};
BindingContext = this;

Path不需要:

<Label Text = "{Binding textSample}" />

答案 1 :(得分:0)

另一种方式

public string textSample {get;set;} = "This is a test!";
相关问题