Xamarin 如何显示导航栏';TitleView&x27;通用于所有页面
我在Xamarin 如何显示导航栏';TitleView&x27;通用于所有页面,xamarin,xamarin.forms,navigationbar,titleview,Xamarin,Xamarin.forms,Navigationbar,Titleview,我在MainPage中添加了TitleView以在Navigationbar上显示,但当我导航到另一个页面Navigationbar显示为空时,它仅显示在MainPage上 下面是我在MainPage.xaml文件中的代码 <NavigationPage.TitleView> <RelativeLayout HorizontalOptions="Fill" <Image Source="bell.png" HeightRequest="25" WidthRe
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>
我写了一个关于你需求的演示。
这是GIF
您可以编写从“ContentPage”继承的自定义页面,并向其添加工具栏项
更新
我是通过DependencyService
实现的,如果你想知道关于如何实现DependencyService的更多细节,你可以参考这个博客和我的代码。
有代码使用了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类工具栏页面:ContentPage
{
公共工具栏项目工具栏项目;
公共静态int项;
公共工具栏页()
{
//公共ToolbarItem(字符串名称、字符串图标、激活的操作、ToolbarItemOrder=ToolbarItemOrder.Default、int priority=0);
toolbarItem=新的toolbarItem();
toolbarItem.Icon=“ring2.png”;
toolbarItem.Order=ToolbarItemOrder.Primary;
//工具栏项。文本=项+“”;
工具栏项。优先级=0;
toolbarItem.Clicked+=toolbarItem\u Clicked;
工具栏项。添加(工具栏项);
如果(项目>=1)
{
DependencyService.Get().SetBadge(this,toolbarItem,$“{item}”,Color.Red,Color.White);
}
}
已单击私有无效工具栏项(对象发送者,事件参数e)
{
项目=项目+1;
DependencyService.Get().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);
}
}
public分部类主页面:工具栏页面
{
公共主页()
{
初始化组件();
bt1.Text=ToolbarPage.item.ToString();
bt1.单击+=异步(o,e)=>
{
wait Navigation.PushAsync(新的HelloToolbarInher());
};
}
受保护的重写异步void OnAppearing()
{
//你必须延期,
等待任务。延迟(100);
bt1.Text=ToolbarPage.item.ToString();
DependencyService.Get().SetBadge(this,toolbarItem,$“{ToolbarPage.item}”,Color.Red,Color.White);
}
}
不要忘记更改主页.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>
这是我的新演示。
您是否将整个标题视图代码也放在了第二页中?为了减少这一工作量,您可以创建一个标题视图的通用控件,并在所有其他页面中使用它,但是你必须在你想显示标题视图的每个页面中使用。是的,这是可以做到的,但我只是想避免在所有页面中都有标题视图。你也可以使用标题视图创建公共内容页面,并在需要时只使用该公共页面,就像你在xaml中声明页面一样。谢谢你的回答。您的想法很好,但在.cs文件中编写复杂的UI很困难。@Arvindraja我更新了我的答案,并使用依赖服务实现了UI。你可以参考一下。