0

我已添加显示,但它仅TitleView在我导航到其他显示为空的页面时显示。MainPageNavigationbarMainPageNavigationbar

下面的代码我在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所有页面的共同点?

4

1 回答 1

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


}

主文件

    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

于 2019-04-25T04:04:53.907 回答