[assembly: ExportRenderer(typeof(ContentPage), typeof(ExtendedPageRenderer))] | |
public class ExtendedPageRenderer : PageRenderer | |
{ | |
public override void ViewWillAppear(bool animated) | |
{ | |
base.ViewWillAppear(animated); | |
var contentPage = this.Element as ContentPage; | |
if (contentPage == null || NavigationController == null) | |
{ | |
return; | |
} | |
var itemsInfo = contentPage.ToolbarItems; | |
var navigationItem = this.NavigationController.TopViewController.NavigationItem; | |
var leftNativeButtons = (navigationItem.LeftBarButtonItems ?? new UIBarButtonItem[] { }).ToList(); | |
var rightNativeButtons = (navigationItem.RightBarButtonItems ?? new UIBarButtonItem[] { }).ToList(); | |
rightNativeButtons.ForEach(nativeItem => | |
{ | |
// [Hack] Get Xamarin private field "item" | |
var field = nativeItem.GetType().GetField("item", BindingFlags.NonPublic | BindingFlags.Instance); | |
if (field == null) | |
{ | |
return; | |
} | |
var info = field.GetValue(nativeItem) as ToolbarItem; | |
if (info != null && info.Priority != 0) | |
{ | |
return; | |
} | |
rightNativeButtons.Remove(nativeItem); | |
leftNativeButtons.Add(nativeItem); | |
}); | |
navigationItem.RightBarButtonItems = rightNativeButtons.ToArray(); | |
navigationItem.LeftBarButtonItems = leftNativeButtons.ToArray(); | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
Hi, |
This comment has been minimized.
This comment has been minimized.
With two buttons (one left and one right) I get an 'Collection modified' exception after the Remove(nativeItem).
Then before the returns I added:
and removed
and in the end changed this line
This prevents the exception... |
This comment has been minimized.
This comment has been minimized.
I modified the code to build new collections instead of modifying the existing collections to avoid the Collection Modified exceptions as mentioned by @KeesAlderliesten. |
This comment has been minimized.
This comment has been minimized.
To prevent the Collection Modified Exception, simply do a rightNativeButtons.ToList().ForEach(nativeItem => This works because now you're iterating over a copy of the list. |
This comment has been minimized.
This comment has been minimized.
Just started using XF-2.2.0* and noticed the private field has been renamed to _item. And by the way, thanks for this peace of code! It's the best way to move the toolbaritem so far! |
This comment has been minimized.
This comment has been minimized.
How about this such as render for Android ? is possible to Arranged toolbar item to Left even order as secondary ? |
This comment has been minimized.
Thanks for this, it´s working correctly. Very helpful