I've created a basic sample app with Xamarin.Forms, where I tried to repoduce a UI like Twitter or Instagram with:
So, the the pages architecture of my app looks like this:
|-- MasterDetailPage
..|--TabbedPage
....|-- NavigationPage
......|-- ContentPage
For achieving this, I've used:
This renderer looks like this:
public class CustomNavigationPageRenderer : MasterDetailPageRenderer
{
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
for (var i = 0; i < toolbar.ChildCount; i++)
{
var imageButton = toolbar.GetChildAt(i) as ImageButton;
var drawerArrow = imageButton?.Drawable as DrawerArrowDrawable;
if (drawerArrow == null)
continue;
bool displayBack = false;
var app = Xamarin.Forms.Application.Current;
//var navPage = ((app.MainPage.Navigation.ModalStack[0] as MasterDetailPage).Detail as NavigationPage);
var detailPage = (app.MainPage as MasterDetailPage).Detail;
if (detailPage.GetType() == typeof(BottomTp.Views.NaxamMainPage))
{
var tabPage = detailPage as BottomTabbedPage;
var curPage = tabPage.CurrentPage;
var navPageLevel = curPage.Navigation.NavigationStack.Count;
if (navPageLevel > 1)
displayBack = true;
}
if (!displayBack)
ChangeIcon(imageButton, Resource.Drawable.icon);
}
}
private void ChangeIcon(ImageButton imageButton, int id)
{
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
imageButton.SetImageDrawable(Context.GetDrawable(id));
imageButton.SetImageResource(id);
}
}
This works well, but there is a last problem when I "return" to the main page:
Here is a short animation to illustrate this issue:
Is there a way to deactivate this animation? How could I fix this?
You need use a custom NavigationPage
. Here is my demo based on yours.
There are three steps to achieve it:
1) Add a folder named CustomControl
, create a class MyNavigationPage
Inherited from NavigationPage
:
namespace BottomTp.CustomControl
{
public class MyNavigationPage : NavigationPage
{
public MyNavigationPage() { }
public MyNavigationPage(Page root) : base(root) { }
}
}
2) change the tag in your NaxamMainPage.xaml
file,
Add xmlns:controls="clr-namespace:BottomTp.CustomControl;assembly=BottomTp"
in your naxam:BottomTabbedPage
tag, and then change your NavigationPage
tag to controls:MyNavigationPage
:
<controls:MyNavigationPage Title="Browse" Icon="md-view-list"
x:Name="NP1">
<x:Arguments>
<views:SfItemsPage />
</x:Arguments>
</controls:MyNavigationPage>
<controls:MyNavigationPage Title="About" Icon="md-help"
x:Name="NP2">
<x:Arguments>
<views:AboutPage />
</x:Arguments>
</controls:MyNavigationPage>
3) create custom render MyCustomNavigationPageRenderer
on Android platform:
[assembly: ExportRenderer(typeof(MyNavigationPage), typeof(MyCustomNavigationPageRenderer))]
namespace BottomTp.Droid.Renderers
{
class MyCustomNavigationPageRenderer : NavigationPageRenderer
{
public MyCustomNavigationPageRenderer(Context c) : base(c)
{ }
protected override Task<bool> OnPopToRootAsync(Page page, bool animated)
{
return base.OnPopToRootAsync(page, false);
}
protected override Task<bool> OnPopViewAsync(Page page, bool animated)
{
return base.OnPopViewAsync(page, false);
}
protected override Task<bool> OnPushAsync(Page view, bool animated)
{
return base.OnPushAsync(view, false);
}
}
}