我创建了一个自定义导航页面来处理后退导航按钮事件。
Click侦听器已正确连接到工具栏,但是由于某种原因更改方向后,不会以某种方式触发OnClick事件。在更改方向之前,它可以正常工作。谁能帮我解决这个问题?我已经尝试了好几个星期。
using Android.Content;
using Android.Support.V7.Widget;
using Android.Support.V4.Widget;
using Android.Views;
using Android.Util;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
using RichyInspect.Renderers;
using RichyInspect.Droid.Renderers;
using Android.Content.Res;
[assembly: ExportRenderer(typeof(VNavigationPage), typeof(VNavigationPage_Android))]
namespace RichyInspect.Droid.Renderers
{
// Source: https://forums.xamarin.com/discussion/87795/onoptionsitemselected-not-invoked-using-masterdetails-page-on-android
public class VNavigationPage_Android : NavigationPageRenderer
{
public VNavigationPage_Android(Context context) : base(context)
{
AutoPackage = false;
}
protected override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
Element page = Element.RootPage;
MasterDetailPage masterDetailPage = null;
System.Diagnostics.Debug.WriteLine("page: " + (page == null).ToString());
masterDetailPage = page as MasterDetailPage;
System.Diagnostics.Debug.WriteLine("masterDetailPage: " + (masterDetailPage == null).ToString());
if (masterDetailPage == null)
return;
var renderer = Platform.GetRenderer(masterDetailPage) as MasterDetailPageRenderer;
System.Diagnostics.Debug.WriteLine("renderer: " + (renderer == null).ToString());
if (renderer == null)
return;
var drawerLayout = (DrawerLayout)renderer;
Toolbar toolbar = null;
System.Diagnostics.Debug.WriteLine("ChildCount: " + ChildCount);
for (int i = 0; i < ChildCount; i++)
{
var child = GetChildAt(i);
toolbar = child as Toolbar;
System.Diagnostics.Debug.WriteLine("ID:" + toolbar?.Id);
}
System.Diagnostics.Debug.WriteLine("toolbar: " + (toolbar == null).ToString());
toolbar.SetNavigationOnClickListener(new MenuClickListener(Element, drawerLayout));
}
protected override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
Element page = Element.RootPage;
MasterDetailPage masterDetailPage = null;
System.Diagnostics.Debug.WriteLine("page: " + (page == null).ToString());
masterDetailPage = page as MasterDetailPage;
System.Diagnostics.Debug.WriteLine("masterDetailPage: " + (masterDetailPage == null).ToString());
if (masterDetailPage == null)
return;
var renderer = Platform.GetRenderer(masterDetailPage) as MasterDetailPageRenderer;
System.Diagnostics.Debug.WriteLine("renderer: " + (renderer == null).ToString());
if (renderer == null)
return;
var drawerLayout = (DrawerLayout)renderer;
Toolbar toolbar = null;
System.Diagnostics.Debug.WriteLine("ChildCount: " + ChildCount);
for (int i = 0; i < ChildCount; i++)
{
var child = GetChildAt(i);
toolbar = child as Toolbar;
System.Diagnostics.Debug.WriteLine("ID:" + toolbar?.Id);
if (toolbar != null)
break;
}
System.Diagnostics.Debug.WriteLine("toolbar: " + (toolbar == null).ToString());
toolbar.SetNavigationOnClickListener(new MenuClickListener(Element, drawerLayout));
}
private class MenuClickListener : Java.Lang.Object, IOnClickListener
{
readonly NavigationPage navigationPage;
private DrawerLayout layout;
public MenuClickListener(NavigationPage navigationPage, DrawerLayout layout)
{
System.Diagnostics.Debug.WriteLine("page: Listening");
this.navigationPage = navigationPage;
this.layout = layout;
}
public void OnClick(Android.Views.View v)
{
System.Diagnostics.Debug.WriteLine("page: Clicked");
var page = navigationPage.CurrentPage as VContentPage;
var master = navigationPage.CurrentPage as MasterDetailHomePage;
if (navigationPage.Navigation.NavigationStack.Count <= 1)
{
layout.OpenDrawer((int)GravityFlags.Left);
}
if (page != null)
{
if (page.CustomBackButtonAction == null)
{
navigationPage?.PopAsync();
}
else
{
page.CustomBackButtonAction.Invoke();
}
}
else if (master != null)
{
master.RetractMaster();
}
else
{
navigationPage?.PopAsync();
}
}
}
}
}
答案 0 :(得分:0)
您的点击事件可能无法正常运行的原因是,在对配置进行更改时调用了onConfigurationChanged之后,您并未将这些值重新分配给对象。
尝试这样的事情:
protected override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
if(newConfig.Orientation==Orientation.Landscape)
//Code for landscape mode
else if(newConfig.Orientation==Orientation.Portrait)
//Code for potrait mode
}