[RenderWith(typeof(_NavigationPageRenderer))]
public class NavigationPage : Page, IPageContainer<Page>, IBarElement, INavigationPageController, IElementConfiguration<NavigationPage>
{
- public static readonly BindableProperty BackButtonTitleProperty = BindableProperty.CreateAttached("BackButtonTitle", typeof(string), typeof(Page), null);
-
public static readonly BindableProperty HasNavigationBarProperty = BindableProperty.CreateAttached("HasNavigationBar", typeof(bool), typeof(Page), true);
- public static readonly BindableProperty HasBackButtonProperty = BindableProperty.CreateAttached("HasBackButton", typeof(bool), typeof(NavigationPage), true);
-
public static readonly BindableProperty BarBackgroundColorProperty = BarElement.BarBackgroundColorProperty;
public static readonly BindableProperty BarTextColorProperty = BarElement.BarTextColorProperty;
}
}
- public static string GetBackButtonTitle(BindableObject page)
- {
- return (string)page.GetValue(BackButtonTitleProperty);
- }
-
- public static bool GetHasBackButton(Page page)
- {
- if (page == null)
- throw new ArgumentNullException("page");
- return (bool)page.GetValue(HasBackButtonProperty);
- }
-
public static bool GetHasNavigationBar(BindableObject page)
{
return (bool)page.GetValue(HasNavigationBarProperty);
public event EventHandler<NavigationEventArgs> Pushed;
- public static void SetBackButtonTitle(BindableObject page, string value)
- {
- page.SetValue(BackButtonTitleProperty, value);
- }
-
- public static void SetHasBackButton(Page page, bool value)
- {
- if (page == null)
- throw new ArgumentNullException("page");
- page.SetValue(HasBackButtonProperty, value);
- }
-
public static void SetHasNavigationBar(BindableObject page, bool value)
{
page.SetValue(HasNavigationBarProperty, value);
using System.Threading.Tasks;
using System.Collections.Generic;
using Xamarin.Forms.Internals;
-using Xamarin.Forms.PlatformConfiguration.TizenSpecific;
using ElmSharp;
-using EButton = ElmSharp.Button;
-using EToolbar = ElmSharp.Toolbar;
-using EToolbarItem = ElmSharp.ToolbarItem;
-using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.NavigationPage;
-using SpecificPage = Xamarin.Forms.PlatformConfiguration.TizenSpecific.Page;
namespace Xamarin.Forms.Platform.Tizen
{
public class NavigationPageRenderer : VisualElementRenderer<NavigationPage>
{
- enum ToolbarButtonPosition
- {
- Left,
- Right
- };
-
const string PartTitle = "default";
- const string PartBackButton = "elm.swallow.prev_btn";
- const string PartLeftToolbar = "title_left_btn";
- const string PartRightToolbar = "title_right_btn";
- const string PartNavigationBar = "navigationbar";
- const string StyleBackButton = "naviframe/back_btn/default";
- const string StyleNavigationBar = "navigationbar";
readonly List<Widget> _naviItemContentPartList = new List<Widget>();
Naviframe _naviFrame = null;
Page _previousPage = null;
TaskCompletionSource<bool> _currentTaskSource = null;
- ToolbarTracker _toolbarTracker = null;
IDictionary<Page, NaviItem> _naviItemMap;
Page CurrentPage => Element.CurrentPage;
{
_naviFrame.AnimationFinished -= OnAnimationFinished;
}
- if (_toolbarTracker != null)
- {
- _toolbarTracker.CollectionChanged -= OnToolbarCollectionChanged;
- }
}
base.Dispose(disposing);
}
_naviItemMap = new Dictionary<Page, NaviItem>();
}
- if (_toolbarTracker == null)
- {
- _toolbarTracker = new ToolbarTracker();
- _toolbarTracker.CollectionChanged += OnToolbarCollectionChanged;
- }
-
if (e.OldElement != null)
{
var navigation = e.OldElement as INavigationPageController;
navigation.RemovePageRequested += OnRemovePageRequested;
navigation.InsertPageBeforeRequested += OnInsertPageBeforeRequested;
- _toolbarTracker.Target = e.NewElement;
_previousPage = e.NewElement.CurrentPage;
}
base.OnElementChanged(e);
// Tizen does not support 'Tint', but only 'BarBackgroundColor'
else if (e.PropertyName == NavigationPage.BarBackgroundColorProperty.PropertyName)
UpdateBarBackgroundColor(CurrentNaviItem);
- else if (e.PropertyName == Specific.HasBreadCrumbsBarProperty.PropertyName)
- UpdateBreadCrumbsBar(CurrentNaviItem);
-
}
void OnPageCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
page.PropertyChanged += NavigationBarPropertyChangedHandler;
}
- void OnToolbarCollectionChanged(object sender, EventArgs eventArgs)
- {
- UpdateToolbarItem(Element.CurrentPage);
- }
-
void NavigationBarPropertyChangedHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// this handler is invoked only for child pages (contained on a navigation stack)
if (e.PropertyName == NavigationPage.HasNavigationBarProperty.PropertyName)
UpdateHasNavigationBar(sender as Page);
- else if (e.PropertyName == NavigationPage.HasBackButtonProperty.PropertyName ||
- e.PropertyName == NavigationPage.BackButtonTitleProperty.PropertyName)
- UpdateHasBackButton(sender as Page);
else if (e.PropertyName == Page.TitleProperty.PropertyName)
UpdateTitle(sender as Page);
- else if (e.PropertyName == SpecificPage.BreadCrumbProperty.PropertyName)
- UpdateBreadCrumbsBar(GetNaviItemForPage(sender as Page));
}
void UpdateHasNavigationBar(Page page)
return;
}
item.TitleBarVisible = (bool)page.GetValue(NavigationPage.HasNavigationBarProperty);
- UpdateToolbarItem(page, item);
UpdateBarBackgroundColor(item);
- UpdateBreadCrumbsBar(item);
- }
-
- void UpdateToolbarItem(Page page, NaviItem item = null)
- {
- if (item == null)
- item = GetNaviItemForPage(page);
-
- if (_naviFrame.NavigationStack.Count == 0 || item == null || item != _naviFrame.NavigationStack.Last())
- return;
-
- Native.Button rightButton = GetToolbarButton(ToolbarButtonPosition.Right);
- item.SetPartContent(PartRightToolbar, rightButton);
-
- Native.Button leftButton = GetToolbarButton(ToolbarButtonPosition.Left);
- item.SetPartContent(PartLeftToolbar, leftButton);
- UpdateHasBackButton(page, item);
- }
-
- void UpdateHasBackButton(Page page, NaviItem item = null)
- {
- if (item == null)
- item = GetNaviItemForPage(page);
-
- EButton button = null;
-
- if ((bool)page.GetValue(NavigationPage.HasBackButtonProperty) && _naviFrame.NavigationStack.Count > 1)
- {
- button = CreateNavigationButton((string)page.GetValue(NavigationPage.BackButtonTitleProperty));
- }
- item.SetPartContent(PartBackButton, button);
}
void UpdateTitle(Page page, NaviItem item = null)
UpdateBarBackgroundColor(item);
}
- void UpdateBreadCrumbsBar(NaviItem item)
- {
- if (Element.OnThisPlatform().HasBreadCrumbsBar())
- {
- item.Style = StyleNavigationBar;
- item.SetPartContent(PartNavigationBar, GetBreadCrumbsBar());
- }
- else
- {
- item.SetPartContent(PartNavigationBar, null, false);
- }
- }
-
- EButton CreateNavigationButton(string text)
- {
- EButton button = new EButton(Forms.NativeParent);
- button.Clicked += (sender, e) =>
- {
- if (!Element.SendBackButtonPressed())
- Forms.Context.Exit();
- };
-
- button.Style = StyleBackButton;
- button.Text = text;
-
- _naviItemContentPartList.Add(button);
- button.Deleted += NaviItemPartContentDeletedHandler;
-
- return button;
- }
-
void NaviItemPartContentDeletedHandler(object sender, EventArgs e)
{
_naviItemContentPartList.Remove(sender as Widget);
return null;
}
- Native.Button GetToolbarButton(ToolbarButtonPosition position)
- {
- ToolbarItem item = _toolbarTracker.ToolbarItems.Where(
- i => (position == ToolbarButtonPosition.Right && i.Order <= ToolbarItemOrder.Primary)
- || (position == ToolbarButtonPosition.Left && i.Order == ToolbarItemOrder.Secondary))
- .OrderBy(i => i.Priority).FirstOrDefault();
-
- if (item == default(ToolbarItem))
- return null;
-
- Native.ToolbarItemButton button = new Native.ToolbarItemButton(item);
- return button;
- }
-
- EToolbar GetBreadCrumbsBar()
- {
- EToolbar toolbar = new EToolbar(Forms.NativeParent)
- {
- Style = StyleNavigationBar,
- ItemAlignment = 0,
- Homogeneous = false,
- ShrinkMode = ToolbarShrinkMode.Scroll
- };
-
- foreach (var p in Element.Navigation.NavigationStack)
- {
- string breadCrumb = p.OnThisPlatform().GetBreadCrumb();
- if (!string.IsNullOrEmpty(breadCrumb))
- {
- EToolbarItem toolbarItem = toolbar.Append(breadCrumb);
- toolbarItem.Selected += (s, e) =>
- {
- var copyOfStack = Element.Navigation.NavigationStack.Reverse().Skip(1);
- foreach (var lp in copyOfStack)
- {
- if (lp == p) break;
- Element.Navigation.RemovePage(lp);
- }
- if (Element.Navigation.NavigationStack.Last() != p)
- Element.Navigation.PopAsync();
- };
- }
- }
-
- return toolbar;
- }
-
void OnPopRequested(object sender, NavigationRequestedEventArgs nre)
{
if ((Element as IPageController).InternalChildren.Count == _naviFrame.NavigationStack.Count)