Xamarin.forms 如何在ContentView更改时动态更改导航栏标题?
我正在用Xamarin开发一个新的非常基本的应用程序。Forms,我有一个疑问。。。我不知道该怎么做,因为我对Xamarin没有太多的经验 我得到了一个Xamarin.forms 如何在ContentView更改时动态更改导航栏标题?,xamarin.forms,Xamarin.forms,我正在用Xamarin开发一个新的非常基本的应用程序。Forms,我有一个疑问。。。我不知道该怎么做,因为我对Xamarin没有太多的经验 我得到了一个ContentPage,在那里我放置了一些ContentView,根据我单击的选项卡,更改第一个选项卡中的导航标题很容易,因为我可以使用title=“[Introduct any title]”,但我在视图中没有此属性 这是我的代码,我添加了我想添加的视图的名称空间,如下所示: <ContentPage ... xmlns:views="c
ContentPage
,在那里我放置了一些ContentView
,根据我单击的选项卡,更改第一个选项卡中的导航标题很容易,因为我可以使用title=“[Introduct any title]”
,但我在视图中没有此属性
这是我的代码,我添加了我想添加的视图的名称空间,如下所示:
<ContentPage
...
xmlns:views="clr-namespace:Your.Name.Space"
...>'
'
然后在我的页面内容中,我可以这样使用它:
<ContentPage
...
xmlns:views="clr-namespace:Your.Name.Space"
...>'
我正在为底部导航使用自定义选项卡布局
我希望你们中的任何人都能用正确的方式来做这件事
谢谢 正如杰拉尔德·弗斯路易斯所说,你必须在
内容页面上设置标题。只需为每个选项卡创建两个ContentPage
s,一个ContentPage
。然后为每个ContentPage
创建绑定
<ContentPage Title="{Binding PageTitleFirstPage}" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="stackoverflow.AboutPage" xmlns:vm="clr-namespace:stackoverflow;">
</ContentPage>
更新
我听说您想通过在ContentView之间切换来更改标题。你能给我们看看你的密码吗?因为您可以在ContentView切换的位置轻松设置页面标题。假设您通过单击切换ContentView,您可以在那里设置页面标题。但您还必须通知视图,此属性已更改。实现此功能的建议方法是在viewmodel上设置一个PageTitle
属性,该属性与当前内容视图保持同步;因此,它相应的名称
但是如果您只想在视图级别实现它;然后,您可以通过在内容视图中引入一个绑定模式为OneWayToSource
的自定义可绑定属性来实现这一点。例如:
public class NavigationView : ContentView
{
public static readonly BindableProperty TitleProperty =
BindableProperty.Create("Title",
typeof(string),
typeof(NavigationView),
defaultValue: null,
defaultBindingMode: BindingMode.OneWayToSource);
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
....
x:Name="_self"
Title="{Binding Path=Content.Title, Source={x:Reference _self}}">
并且,使用自引用将自定义属性绑定到ContentPage
title。例如:
public class NavigationView : ContentView
{
public static readonly BindableProperty TitleProperty =
BindableProperty.Create("Title",
typeof(string),
typeof(NavigationView),
defaultValue: null,
defaultBindingMode: BindingMode.OneWayToSource);
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
....
x:Name="_self"
Title="{Binding Path=Content.Title, Source={x:Reference _self}}">
编辑1:还添加了一些代码来说明如何定义从内容视图到页面的绑定。ContentPage
有一个Title
属性,您需要设置它。我知道@GeraldVersluis,但我想在ContentView
之间切换时更改这个Title
。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TitleView"
x:Class="TitleView.TitleViewPage"
x:Name="_self"
Title="{Binding Path=Content.Title, Source={x:Reference _self}}">
<local:NavigationView Title="This is page-1">
<Button Text="Go to page-2" Clicked="Handle_Clicked" />
</local:NavigationView>
</ContentPage>
public partial class TitleViewPage : ContentPage
{
void Handle_Clicked(object sender, System.EventArgs e)
{
this.Content = new NavigationView {
Title = "This is page-2",
Content = new Button { Text = "Go back to page-1", IsEnabled = false } };
}
public TitleViewPage()
{
InitializeComponent();
}
}