简体   繁体   English

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

[英]How to dynamically changed ContentPage Title inside XAML in xamarin forms?

I have a variable var wordsCount = App.words.Count.ToString(); 我有一个变量var wordsCount = App.words.Count.ToString(); inside my OnAppearing() method in C#. 在我的C#中的OnAppearing()方法中。 How can I pass the value of the wordsCount to the title property of the contentpage in the XAML side so everytime I go to that page the title is updated accordingly? 如何将wordsCount的值传递给XAML端内容页面的title属性,以便每次进入该页面时标题都会相应更新? Kind of like the below code: 有点像下面的代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        x:Class="Japanese.PhrasesPage"
        Title="wordsCount">
</ContentPage>

In the code behind of your page override OnAppearing() and set the Title property: 在页面后面的代码覆盖OnAppearing()并设置Title属性:

override void OnAppearing()
{
  Title = wordsCount;
}

If you would like to use bindings, you need to set the BindingContext and make your field a public property: 如果您想使用绑定,则需要设置BindingContext并使您的字段成为公共属性:

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

  public string WordCount { get { return wordCount; }}
}

And in XAML: 在XAML中:

Title="{Binding WordCount}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM