【问题标题】:How to dynamically change the navigation bar title when ContentView changes?ContentView改变时如何动态改变导航栏标题?
【发布时间】:2017-09-16 03:09:29
【问题描述】:

我正在使用 Xamarin.Forms 开发一个新的非常基本的应用程序,但我有一个疑问......我无法弄清楚如何做到这一点,因为我对 Xamarin 没有太多经验。

我有一个ContentPage,根据我单击的选项卡,我可以在其中放置一些ContentView,更改第一个导航标题很容易,因为我可以使用Title="[introduce any title]",但我没有这个属性意见。

这基本上是我的代码,我添加了我想要添加的视图的命名空间,如下所示:

<ContentPage
...
xmlns:views="clr-namespace:Your.Name.Space"
...>'

然后在我的页面内容中,我可以这样使用它:

&lt;views:NameOfView /&gt;

我正在为底部导航使用自定义选项卡布局。

我希望你们中的任何人都可以采取正确的方式。

谢谢。

【问题讨论】:

  • ContentPage 有一个 Title 属性,您需要设置它。
  • 我知道@GeraldVersluis,但是当我在ContentView 之间切换时,我想更改这个Title

标签: xamarin.forms


【解决方案1】:

推荐的实现方式是在您的视图模型上拥有一个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 标题。例如:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
    ....
    x:Name="_self"
    Title="{Binding Path=Content.Title, Source={x:Reference _self}}">

或者,为自定义视图添加绑定以充分利用绑定模式:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
    ...
    x:Name="_self">
    ...
    <local:NavigationView Title="{Binding Path=Title, Source={x:Reference _self}}">

使用示例

XAML:

<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();
    }
}

编辑 1: 还添加了一些代码来说明如何定义从内容视图到页面的绑定。

【讨论】:

    【解决方案2】:

    正如 Gerarld Versluis 所说,您必须在 ContentPage 上设置标题。只需为每个标签一个 ContentPage 创建两个 ContentPages。然后为每个ContentPage 创建Binding。

    <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>
    

    对于第二个 ContentPage 也一样,如果你愿意,你也可以在 ContentPage 中使用 ContentView!

    <ContentPage Title="{Binding PageTitleSecondPage}" 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>
    

    在 ViewModel 中使用您想要的字符串设置一个属性:

    public string PageTitleSecondPage => "Second Tab";
    

    更新 我读到您想通过在 ContentView 之间切换来更改标题。你能告诉我们你的代码吗?因为您可以轻松地将 PageTitle 设置在 ContentView 切换的位置。假设您通过单击切换 ContentView,您可以在那里设置 PageTitle。但是您还必须通知 View,该属性已更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-22
      • 2022-01-12
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 2018-06-06
      • 2019-09-11
      • 1970-01-01
      相关资源
      最近更新 更多