我已经使用Xamarin.Forms 创建了一个基本示例应用,我尝试使用以下内容重新创建Twitter或Instagram等用户界面:
因此,我的应用的页面架构如下所示:
| - MasterDetailPage
.. | - TabbedPage
.... | - NavigationPage
...... | - ContentPage
为实现这一目标,我使用了:
此渲染器如下所示:
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);
}
}
这很有效,但当我“返回”主页时,最后一个问题:
这是一个简短的动画来说明这个问题:
有没有办法停用此动画?我该如何解决这个问题?
答案 0 :(得分:2)
您需要使用自定义NavigationPage
。这是基于你的demo。
实现它有三个步骤:
1)添加名为CustomControl
的文件夹,创建一个类MyNavigationPage
继承自NavigationPage
:
namespace BottomTp.CustomControl
{
public class MyNavigationPage : NavigationPage
{
public MyNavigationPage() { }
public MyNavigationPage(Page root) : base(root) { }
}
}
2)更改NaxamMainPage.xaml
文件中的标记
在xmlns:controls="clr-namespace:BottomTp.CustomControl;assembly=BottomTp"
代码中添加naxam:BottomTabbedPage
,然后将NavigationPage
代码更改为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)在Android平台上创建自定义渲染MyCustomNavigationPageRenderer
:
[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);
}
}
}