How to change the Back Button Text on Xamarin.Forms - xamarin

I have been trying to change the text of the back button in Xamarin.Forms without luck.
I tried already this code with no luck:
NavigationPage.SetBackButtonTitle(this, "");
I'm using a MasterDetailPage.
I tried to add the code into the constructor of the XAML cs file.
Libraries used: PRISM 6.2.0, Xamarin Forms 2.3.1.114
Any suggestion or idea?
Thanks in advance

Due to the topic:
One of the things that is a little bit unintuitive about this API is that you are setting the title for the back button that shows up when this page is the one that you are going back to. For example, you are trying to set the title to "Home". That means you should be calling this method on the Page that represents the "home" page, not the page that is visible when the back button that says "Home" is showing.
So, if you are navigating from Page1 to Page2, you should set NavigationPage.SetBackButtonTitle(this, "") in constructor of Page1 and on Page2 you will see the empty title of back button.

You have to set the Backbutton title of your previous page as string.Empty. By default it's the title of the previous page on the navigation stack that is shown as the back button text.
Also Android does not support this property.

You can also do it from the Code Behind of the current xaml page
public TodoPage()
{
InitializeComponent();
NavigationPage.SetBackButtonTitle(this, "Back");
}
NB: It has to be done on the previous page you want to set.
e.g. Page A has to have the code if you want Page B to show "Back" title.

Belated answer but maybe someone will find this useful in the future… Anyhow, if you wish to get rid of the back button title from XAML, you can either add NavigationPage.BackButtonTitle="" for any ContentPage that should have the title removed or you can define new ContentPage style in your App.xaml file like this:
<Style x:Key="ContentPageStyle" TargetType="ContentPage">
<Setter Property="BackgroundColor" Value="White" /><!-- just an example of other things that can be in here -->
<Setter Property="NavigationPage.BackButtonTitle" Value="" />
</Style>
I was, however, unable to turn this into an implicit global style that would get applied automatically without the need to specify Style="{StaticResource ContentPageStyle}" for each ContentPage – I'm not sure why that one doesn't work.

You can set the attribute NavigationPage.BackButtonTitle on ContentPage
For example:
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:T3R"
mc:Ignorable="d"
Title="Title of Page"
NavigationPage.BackButtonTitle="Cancel"
x:Class="T3R.MainPage">
Remember the following:
I understand this only works on iOS
For any given page, the back button title is set by the page that presents it. This way, the button label can vary with respect to the page that precedes it on the navigation stack (of which there can be many).

You can set your back button text in XAML as follows (before ContentPage.Content):
<NavigationPage.TitleView>
<Label Text="{Binding PageTitle}" HorizontalOptions="Center"/>
</NavigationPage.TitleView>

Related

How do I navigate from MasterDetailPage to ContentPage in Xamarin forms using Prism saving the navigation stack?

I'm working on a Xamarin+Prism app and now facing an issue. The application uses MasterDetailPage as a layout. The details page contains a google map with few markers. When you tap the marker, a popup appears, if you tap this popup, a separate view should be opened (let's call it Page1).
I want this view NOT to be a part of MasterDetailsPage and at the same time to have a "Back" button on the top navigation panel. This "Back" button should lead back to MasterDetailsPage. And here comes the issue. I haven't found a way to do this. If you don't use Prism, it's done very simple by pushing directly to NavigationStack. But Prism doesn't allow you to do so, you only have NavigationService with NavigateAsync. Is there a way to implement this kind of behavior? Or maybe any hacks?
MasterDetailsPage code
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Find.Views.MainPage"
xmlns:pages="clr-namespace:Find.Views">
<MasterDetailPage.Master>
<pages:MenuPage />
</MasterDetailPage.Master>
</MasterDetailPage>
I've tried few ways of navigation like:
await NavigationService.NavigateAsync($"Page1");
await NavigationService.NavigateAsync($"/Page1");
await NavigationService.NavigateAsync($"NavigationPage/Page1");
and others, but nothing worked.
I've found the similar question, but an answer there is not correct.
How do you navigate Xamarin Forms using Prism from Master Detail to Content Page without Master Detail
Do you want to achieve the result like following GIF?(Note: I will navigate a page1 when click the Button)
First of all, I registered my pages in RegisterTypes method like following code.
protected override void RegisterTypes(IContainerRegistry containerRegistry) {
containerRegistry.RegisterForNavigation<MenuPage>();
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<ViewA>();
containerRegistry.RegisterForNavigation<Page1>();
containerRegistry.RegisterForNavigation<ViewB>();
}
If you want to have a back arrow, you need keep your navigted page not in root navigate stack.
For example. I display the ViewA by default, this navigate code like following
await _navigationService.NavigateAsync(nameof(NavigationPage) + "/" + "ViewA");
Then, when we want to navigate to the Page1 from ViewA, we should use following code that will display the backarrow.
await _navigationService.NavigateAsync(nameof(NavigationPage) + "/" + "ViewA" + "/" + "Page1");
I upload my demo, you can refer to it.
https://github.com/851265601/XFromsPrismNavi

Duplicate Xamarin Navbar Toolbar Items

I'm making an app in Xamarin Forms and when I deploy it to Android, I get a weird problem. It appears that I have two toolbar items when I only declared one in XAML. Also, I believe this is proper behaviour, but I was wondering if it's possible to not display the navbar title on a lower line and rather display it upper center. Thanks!
Screenshot
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Biziboards.Views.MerchantListPage"
Title="Merchants">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Home"
x:Name="btnHome"
Clicked="btnHome_Clicked"
Order="Primary"/>
</ContentPage.ToolbarItems>
In your pages Xaml.cs file:
NavigationPage.SetHasNavigationBar(this, false);

How to hide or remove particular tableSection(Xamarin.forms) which created by xaml?

I'm making app with using Xamarin.form.
I created tableview that has three sections from xaml.
And I want to hide or remove last section (entire section, with sectionTitle).
But unfortunately, Xamarin xaml doesn't support conditional processing. (only works if element has isVisible property but tableSection does not have it)
Is there any option I can do?
Thanks.
Yes you can remove a section dynamically doing the following:
XAML:
<TableView x:Name="Table">
<TableSection x:Name="Section">
<TextCell Text="something"/>
</TableSection>
<TableSection x:Name="Section2">
<TextCell Text="something2"/>
</TableSection>
</TableView>
Code Behind:
Table.Root.Remove(Section);
-OR-
Table.Root.Remove(0); //If you know the index of the section
If you need to add it back at some point, be sure to store it in a variable in you code behind before removing it like so:
TableSection section = Table.Root[0];
-OR-
TableSection section = Table.Root.IndexOf(Section);
TableSection section = Table.Root.IndexOf(Section);
is wrong because IndexOf returns int
Try
TableSection section = Table.Root[Table.Root.IndexOf(Section)];

How to add a button to first item in ListView

Greeting,
I'm developing an apps for Windows Phone 8.1 and face some problem with ListView.
I wanted to place a button for the FIRST item in ListView, but it seem like I can't align center the button.
Below is the code I use currently:
<ListView>
<Button Content="Jio!" Height="6" Width="362"/>
<ListViewItem Content="ListViewItem"/>
</ListView>
Adding horizontalalignment='center' just wont work for the button.
The reason I want to do this is because I wanted the button to scroll together with the list, hence I'm placing it inside the ListView.
Please advice what can I do to achieve my purpose, thanks!
I recommend using the ListView.Header content to place such a button instead of adding it as a child directly.
<ListView>
<ListView.Header>
<Button ... HorizontalAlignment="Center" />
</ListView.Header>
</ListView>
By default, the ListViewItems are left-aligned. You will eventually have to replace their Template in order to center-align it (HeaderTemplate Property).

How to setup a content page of a tabbed page in XAML in Xamarin?

I want to setup a tabbed page with three content pages they are page1, page2 and page3 all of them are content pages. When the program starts I want to show page1 content page with page1 tab selected. They do not follow item template model since they are all different content pages compared to the example showing in the Tabbed Page example # xamarian.
<TabbedPage.Children>
<ContentPage Title="Page 1" />
<ContentPage Title="Page 2" />
<ContentPage Title="Page 3" />
</TabbedPage.Children>
What property I should set here so that I can point to the content of the associated content page or do I follow tab selecte event and manually call the appropriate content page? I would like to try to do it with XAML as much as possible.
Thanks
Found the answer here just in case looking for the same question. In my case, I was able to do it in the code behind, but you can do the same in XAML as the post says. One additional thing is, when you add a content page to a tab item and if you want to add title and Icon to it, then you can reference the index of the child and you can set them manually as .Tile and .Icon.
this.Children.Add( new Page1 ());
this.Children[0].Title = "Page 1";
this.Children[1].Icon = "page1.png"

Resources