【问题标题】:How to dynamically changed ContentPage Title inside XAML in xamarin forms?如何在 xamarin 表单中动态更改 XAML 中的 ContentPage Title?
【发布时间】:2016-09-23 07:07:32
【问题描述】:

我在 C# 中的 OnAppearing() 方法中有一个变量 var wordsCount = App.words.Count.ToString();。如何将 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>

【问题讨论】:

    标签: c# xaml xamarin.forms


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      相关资源
      最近更新 更多