如何在xamarin表单中动态更改XAML中的ContentPage Title

本文关键字:XAML 中的 ContentPage Title 动态 xamarin 表单 | 更新日期: 2023-09-27 18:08:38

我有一个变量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>

如何在xamarin表单中动态更改XAML中的ContentPage Title

在你的页面后面的代码覆盖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}"