C# 如何在xamarin表单中动态更改XAML中的ContentPage标题?
我有一个变量C# 如何在xamarin表单中动态更改XAML中的ContentPage标题?,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,我有一个变量var-wordscont=App.words.Count.ToString()。如何将WordScont的值传递给XAML端contentpage的title属性,以便每次访问该页面时标题都会相应地更新?有点像下面的代码: <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
var-wordscont=App.words.Count.ToString()。如何将WordScont的值传递给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>
在页面的代码隐藏中覆盖OnAppearing()
,并设置标题
属性:
override void OnAppearing()
{
Title = wordsCount;
}
public class MyPage : ContentPage
{
public ContentPage()
{
BindingContext = this;
}
public string WordCount { get { return wordCount; }}
}
如果要使用绑定,需要设置BindingContext
,并将字段设置为公共属性:
override void OnAppearing()
{
Title = wordsCount;
}
public class MyPage : ContentPage
{
public ContentPage()
{
BindingContext = this;
}
public string WordCount { get { return wordCount; }}
}
在XAML中:
Title=“{Binding WordCount}”