2017-03-23 26 views
0

我有一個非常簡單的代碼示例來嘗試測試數據綁定的一個方面。但是,我無法讓它工作,我希望有人能指出我做錯了什麼。簡單的Xamarin表單文本綁定在代碼後面不起作用

在CS代碼隱藏文件我有這樣的:

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

而在XAML文件:

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

當我運行代碼,標籤不顯示任何內容。

我明顯錯過了一些東西,但我看不到什麼。不需要

提前

感謝所有幫助

回答

1

只能綁定到公共properties

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

Path

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

另一種方式

public string textSample {get;set;} = "This is a test!";