I want to change the icon of the back button on the Navigation Bar in .Net MAUI. When I want to customize it with TitleView I don't get the result I want. Is there a way to change the icon of just this button?
我想在.Net Maui中更改导航栏上的后退按钮的图标。当我想用标题视图自定义它时,我得不到我想要的结果。有没有办法只改变这个按钮的图标?
I tried to make a custom bar with TitleView, but I didn't get the result I wanted.
我试图用标题视图制作自定义栏,但没有得到我想要的结果。
更多回答
Umm hide the existing one and use TitleView and adjust things i guess?
嗯,隐藏现有的,并使用标题查看和调整内容,我猜?
From document Back button behavior:
从文档返回按钮行为:
Back button appearance and behavior can be redefined by setting the
BackButtonBehavior
attached property to a BackButtonBehavior
object.
The following code shows an example of redefining back button appearance and behavior:
下面的代码显示了重新定义后退按钮外观和行为的示例:
<ContentPage ...>
<Shell.BackButtonBehavior>
<BackButtonBehavior Command="{Binding BackCommand}"
IconOverride="back.png" />
</Shell.BackButtonBehavior>
...
</ContentPage>
The Command
property is set to an ICommand
to be executed when the back button is pressed, and the IconOverride
property is set to the icon that's used for the back button.
Command属性设置为按下Back按钮时要执行的ICommand,IconOverride属性设置为用于Back按钮的图标。
You can also customize the TitleView
by yourself. Here, we need to hide the default navigation bar and add Shell.TitleView
tag.
您也可以自己自定义标题视图。在这里,我们需要隐藏默认导航栏并添加Shell.TitleViewtag。
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xaminals.Views.BearDetailPage"
Title="Bear Details">
<Shell.BackButtonBehavior>
<BackButtonBehavior IsVisible="False" />
</Shell.BackButtonBehavior>
<Shell.TitleView>
<HorizontalStackLayout>
<ImageButton Source="back.png" Margin="0,0,5,0"></ImageButton>
<Label Text="test title" FontSize="16" VerticalTextAlignment="Center" TextColor="LightSalmon" Margin="0,0,10,0"></Label>
</HorizontalStackLayout>
</Shell.TitleView>
</ContentPage>
Note:
You can check the official sample here Shell.
注:您可以在这里查看官方的贝壳样品。
更多回答
I have a question for your first answer. Is there any way to change the font family of the back button? I am using the Material Icons font. 2. I tried this method, but there is an uncomfortable gap where the back button is.
我有一个问题要问你的第一个答案。有什么方法可以改变后退按钮的字体系列吗?我使用的是素材图标字体。2.我试过这种方法,但后退按钮的位置有一个令人不舒服的缺口。
I tried the Arrow back iOS new
icon of Material+Symbols , and added it to my code and replaced the default icon on my app, and it looks well on my side.
我尝试了Arrow Back iOS的新图标+符号,并将其添加到我的代码中,并替换了应用程序的默认图标,它看起来很适合我。
I want to add it as font and enter it as unicode data.
我想将其添加为字体并输入为Unicode数据。
This depends on the font image you are using, and you may consider using other images instead.
这取决于您使用的字体图像,您可以考虑使用其他图像。
我是一名优秀的程序员,十分优秀!