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

2 c# xaml xamarin.forms

我var wordsCount = App.words.Count.ToString();在C#中的OnAppearing()方法中有一个变量.如何将wordsCount的值传递给XAML端内容页面的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>
Run Code Online (Sandbox Code Playgroud)

Kru*_*lur 7

在页面覆盖的代码后面OnAppearing()并设置Title属性:

override void OnAppearing()
{
  Title = wordsCount;
}
Run Code Online (Sandbox Code Playgroud)

如果您想使用绑定,则需要设置BindingContext并使您的字段成为公共属性:

public class MyPage : ContentPage
{
  public ContentPage()
  {
    BindingContext = this;
  }

  public string WordCount { get { return wordCount; }}
}
Run Code Online (Sandbox Code Playgroud)

在XAML中:

Title="{Binding WordCount}"