How to override back button Title - xamarin

I have a Xamarin Forms project, where the Android part is working fine. But in the navigation bar of my iOS App I have the problem that the title of some pages are too long.
So what I want to do is just replacing the title of the back button with the title of the page.
I already have a custom NavigationRenderer for some other adjustments in iOS.
In the ViewDidAppear Method of the NavigationRenderer I have tried:
NavigationItem.BackBarButton.Title = NavigationItem.Title;
NavigationItem.Title = "";
But it didn't changed anything. I also tried this with the RootController and even tried this with overriding in a custom ViewController. Nothing worked so far.
So my question is how can I accomplish this? Is the NavigationRenderer the wrong place to do it?

You generally set the back button title in the page the back button refers to i.e. the one before the page that is now showing.
NavigationPage.SetBackButtonTitle(this, "My Short Title");
So you would have to set it before you navigate to the new page if you want the new page title as the back button title.

Related

Xamarin resets Detail Property of MasterDetailPage/FlyoutPage when power saving mode is turned on

I have a weird problem with my Xamarin App. When I turn on the power saving mode the App resets the Detail Property of my MasterDetailPage and thus navigate back to the Loginscreen. I cant really debug that.
My Navigation works like that: I create a new NavigationPage and set the Detail Property of my MasterDetailPage to the new NavigationPage.
Does anyone know why this happens?
//Edit using FlyoutPage instead of MasterDetailPage doesn't work
//Edit2 apparently that only happens on my Zebra TC26. Also it not only resets the Detail property to its default value it resets ALL variables/properties in my entire app except static ones to its default value...

How can I prevent additional navigation bar on top of MasterDetailPage (Xamarin on Android)

I am creating an application in Xamarin Forms (4.0) for Android (Android 8.1).
My main page is a MasterDetailPage where I set the detail page to:
this.Detail = new NavigationPage(new SomePage());
When I want to navigate the detail to another page (so that the 'back' button works correctly) I just do:
this.Detail.Navigation.PushAsync(new NavigationPage(new SomeOtherPage());
This all works fine but I am left with an additionl navigation bar with the back button:
If I do NavigationPage.SetHasBackButton(this, false); the back button goes away but the navigation bar is left so I have a big blue rectangle on top of my page. If I do NavigationPage.SetHasNavigationBar(this, false); then both the navigation bar and the titlebar (with hamburger menu) dissappear! Is there a way to just hide the navigation bar with the back button but leave the master/detail title bar (with hamburger menu)?
It seems that the way I've tried to make it work is not really supported by Xamarin Forms. If you navigate the Detail away to another page you effectively loose access to the Master part of the MasterDetailPage (at least on some devices). My attempt to avoid this by wrapping the target Detail page into another NavigationPage works in the Emulator but that is simply by accident.
you only need one instance of NavigationPage
this.Detail.Navigation.PushAsync(new SomeOtherPage());
You can assign the new MasterDetailPage to the root and then handle back pressed. As this isn't suppose to work you must hack a bit.
Make sure that your MainPage is set only to MasterDetailPage.
Once, I had a case that somebody did something like this:
MainPage = new NavigationPage(new MasterDetailPage());
Of course, it should be defined like:
MainPage = new MasterDetailPage();
Then, your DetailPage should be defined only once as it follows:
Detail = new NavigationPage(new MyPage());

Prism Xamarin Forms ToolbarItem appearing twice

I am using Prism for Xamarin Forms application development.
May be because I am new to both Prism and Xamarin Froms because of which I am facing a basic issue with the navigation.
Let me start with the details of my implementation and then the issue at hand.
I have a MasterDetail Page (named Home) which is my main page.
There are a few menu items in the Master Page. One of them is Partner.
On Click of Partner menu item, NavigationService.NavigateAsync("Navigation/Partner") method is called. Where "NavigationService" is of type "INavigationService".
This opens a page called "Partner" which is a tabbed page (TabbedPage). The first tab is a contentpage called "PartnerAll".
PartnerAll page contains a listview. On click of a list view item, a new page is opened "PartnerDetails" by calling NavigationService.NavigateAsync("Navigation/PartnerDetails", parameters, false, true);
On PartnerDetails page, I have added a toolbaritem called "Cancel".
Issue: When "PartnerDetails" page opens up, "Cancel" item shows up twice, as shown in the screenshot:
screenshot of partner details page with two cancel buttons
Where is it I am going wrong. What should I do to make it work?
Please assist.
Best regards, Ankur Jain
You’re pushing two NavigationPage’s onto the Navigation Stack. This would result in two Navigation bars. You can see in your screenshot you have both a back and a hamburger menu icon.
You should just pass in the identifier string for your destination page, instead of pushing another instance of the NavigationPage onto the stack
do it like so NavigationService.NavigateAsync(PartnerDetails);

How do you navigate Xamarin Forms using Prism from Master Detail to Content Page without Master Detail

I am using Xamarin Forms with Prism. How do you navigate from the MasterDetail.Master to a ContentPage that is not part of the MasterDetail (that is, I don't want to just update the detail and continue to be part of the Master/Detail relationship)? The use case is like the Gmail app when you click on Settings from the Hamburger menu. It takes you out of the Master/Detail and you are now in a NavigationPage/ContentPage with a back button to get back to the MasterDetail page.
If you were not using Prism you could go to the Detail page and do a Navigation.PushAsync from there:
var mdp = Application.Current.MainPage as MasterDetailPage;
mdp.IsPresented = false;
await mdp.Detail.Navigation.PushAsync(new ContentPage2());
But I don't see how to do this using Prism navigation.
-Steve
Assuming your Application.Current.MainPage is a MasterDetailPage. Current Detail in the MasterDetailPage is NavigationPage(new ContentPage1()).
In your ContentPage1, you have 2 options to navigate to ContentPage2:
Option 1: Show ContentPage2 in current navigation stack
You will be pushing ContentPage2 into the same Navigation stack of ContentPage1. Back button in navigation bar will be added automatically.
// Call this code in ContentPage1
_navigationService.PushAsync("ContentPage2");
Option 2: Show ContentPage2 modally
You are presenting the page modally and in a completely new navigation stack. You will need to add Back button in the NavigationBar and handle the click event with your own code.
// Call this code in ContentPage1
_navigationService.PushAsync("NavigationPage/ContentPage2", null, true);

Xamarin Forms - Change back button image

I can successfully hide the back button but can't change image of button.
To hide back button:
NavigationPage.SetHasBackButton(page, bool);
There is Icon attribute but we need to change back button image.
The only way to do this is to override the NavigationBar on each particular OS you have.
For Android you will need to look at the ToolBar and on iOS is the NavigationBar.
Refer :
https://blog.xamarin.com/android-tips-hello-toolbar-goodbye-action-bar/
https://developer.xamarin.com/recipes/ios/content_controls/navigation_controller/change_the_back_button/

Resources