gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:55:03 25 4
gpt4 key购买 nike

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

最佳答案

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

override void OnAppearing()
{
Title = wordsCount;
}

如果您想使用绑定(bind),您需要设置 BindingContext 并将您的字段设为公共(public)属性:

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

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

在 XAML 中:

Title="{Binding WordCount}"

关于c# - 如何在 xamarin 表单中动态更改 XAML 中的 ContentPage 标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39654687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com