【问题标题】:How to show Navigationbar 'TitleView' Common for all pages如何显示所有页面通用的导航栏“TitleView”
【发布时间】:2019-04-24 12:24:10
【问题描述】:

我在MainPage 中添加了TitleView 以显示在Navigationbar 上,但当我导航到其他页面Navigationbar 显示为空时,它仅显示MainPage

下面的代码我在 MainPage.xaml 文件中

<NavigationPage.TitleView>
<RelativeLayout  HorizontalOptions="Fill" 
    <Image Source="bell.png"  HeightRequest="25" WidthRequest="25" x:Name="imgBell"
           RelativeLayout.YConstraint="{ConstraintExpression
         Type=RelativeToParent,
         Property=Height,
         Factor=0.018,Constant=10}">
        <Image.GestureRecognizers>
            <TapGestureRecognizer  Command="{Binding GetStaffAnnouncementCommand}"></TapGestureRecognizer>
        </Image.GestureRecognizers>
    </Image>

        <Label  FontSize="10" HorizontalTextAlignment="Center"  VerticalTextAlignment="Center"  BackgroundColor="Transparent" Text="2"    TextColor="Red"
                HeightRequest="22" WidthRequest="23" x:Name="labelText">
    </Frame>

</RelativeLayout>
</NavigationPage.TitleView>

当我点击 铃铛图标 并移动到第二页时 TitleView 根本不显示

如何显示所有页面的通用TitleView

【问题讨论】:

  • 您是否也将整个标题视图代码放在第二页中?
  • 为了减少这项工作,您可以创建一个标题视图的通用控件并在所有其他页面中使用它,但是您必须使用 在要显示标题视图的每个页面中。
  • 是的,这可以做到,但我只是想避免在所有页面中都有 TitleView。
  • 您还可以创建带有标题视图的通用 ContentPage,并在需要时仅使用该通用页面,就像您在 xaml 中声明页面一样。

标签: xamarin xamarin.forms navigationbar titleview


【解决方案1】:

我写了一个关于您的需求的演示。 有 GIF。

您可以编写一个继承自“ContentPage”的自定义页面并向其添加工具栏项。

更新

我是通过DependencyService实现的,如果你想了解更多关于如何实现DependencyService的细节,可以参考这篇博客和我的代码。 https://www.xamboy.com/2018/03/08/adding-badge-to-toolbaritem-in-xamarin-forms/

有代码使用DependencyService

自定义ToolbarPage

 public class ToolbarPage : ContentPage
{
    public ToolbarItem toolbarItem;
    public static int item;
    public ToolbarPage()
    {

        // public ToolbarItem(string name, string icon, Action activated, ToolbarItemOrder order = ToolbarItemOrder.Default, int priority = 0);
         toolbarItem =new ToolbarItem();
        toolbarItem.Icon = "ring2.png";

        toolbarItem.Order = ToolbarItemOrder.Primary;
       // toolbarItem.Text = item+"";
        toolbarItem.Priority = 0;

        toolbarItem.Clicked += ToolbarItem_Clicked;
        ToolbarItems.Add(toolbarItem);
        if (item >= 1)
        {
            DependencyService.Get<IToolbarItemBadgeService>().SetBadge(this, toolbarItem, $"{item}", Color.Red, Color.White);
        }
    }

    private void ToolbarItem_Clicked(object sender, EventArgs e)
    {
        item = item + 1;
        DependencyService.Get<IToolbarItemBadgeService>().SetBadge(this, toolbarItem, $"{item}", Color.Red, Color.White);
    }


}

Main.cs

    public partial class MainPage : ToolbarPage
{
    public MainPage()
    {
        InitializeComponent();
        bt1.Text = ToolbarPage.item.ToString();
        bt1.Clicked += async (o, e) =>
        {

           await Navigation.PushAsync(new HelloToolbarInher());
        };
    }
    protected override async void OnAppearing()
    {
        //You must make a delay,
        await  Task.Delay(100);
        bt1.Text = ToolbarPage.item.ToString();
        DependencyService.Get<IToolbarItemBadgeService>().SetBadge(this, toolbarItem, $"{ToolbarPage.item}", Color.Red, Color.White);
    }
 }

不要忘记更改 MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<local:ToolbarPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:NaviagationViewDemo"
         x:Class="NaviagationViewDemo.MainPage">

<StackLayout>
    <!-- Place new controls here -->
    <Button
        x:Name="bt1"
        Text="click"
        ></Button>
</StackLayout>

这是我的新演示。

https://github.com/851265601/NewNaviagationViewDemo

【讨论】:

  • 感谢您的回答。你的想法很好,但是在 .cs 文件中编写复杂的 UI 很困难。
  • @Arvindraja 我更新了我的答案,并通过依赖服务实现了 UI。你可以参考一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多