我有一个变量var wordsCount = App.words.Count.ToString();
在我的onappear()方法在c#。如何将wordsCount的值传递给XAML中contentpage的title属性,以便每次我转到该页面时,标题都会相应地更新?有点像下面的代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Japanese.PhrasesPage"
Title="wordsCount">
</ContentPage>
在你的页面后面的代码覆盖OnAppearing()
并设置Title
属性:
override void OnAppearing()
{
Title = wordsCount;
}
如果您想使用绑定,您需要设置BindingContext
并使您的字段成为公共属性:
public class MyPage : ContentPage
{
public ContentPage()
{
BindingContext = this;
}
public string WordCount { get { return wordCount; }}
}
在XAML中:
Title="{Binding WordCount}"