C# 如何在xamarin表单中动态更改XAML中的ContentPage标题?

C# 如何在xamarin表单中动态更改XAML中的ContentPage标题?,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,我有一个变量var-wordscont=App.words.Count.ToString()。如何将WordScont的值传递给XAML端contentpage的title属性,以便每次访问该页面时标题都会相应地更新?有点像下面的代码: <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

我有一个变量
var-wordscont=App.words.Count.ToString()。如何将WordScont的值传递给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()
,并设置
标题
属性:

override void OnAppearing()
{
  Title = wordsCount;
}
public class MyPage : ContentPage
{
  public ContentPage()
  {
    BindingContext = this;
  }

  public string WordCount { get { return wordCount; }}
}
如果要使用绑定,需要设置
BindingContext
,并将字段设置为公共属性:

override void OnAppearing()
{
  Title = wordsCount;
}
public class MyPage : ContentPage
{
  public ContentPage()
  {
    BindingContext = this;
  }

  public string WordCount { get { return wordCount; }}
}
在XAML中:

Title=“{Binding WordCount}”