|
using System.Collections.Generic; |
|
using System.Collections.ObjectModel; |
|
using Xamarin.Forms; |
|
|
|
namespace Smallgeek.Controls |
|
{ |
|
public class NavigationBar : ContentView |
|
{ |
|
private readonly Label title; |
|
private readonly Button back; |
|
private readonly StackLayout menuItemView; |
|
|
|
public NavigationBar() |
|
{ |
|
var main = new Grid(); |
|
main.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); |
|
main.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Star }); |
|
main.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); |
|
|
|
main.Padding = new Thickness(10, 18, 18, 0); |
|
|
|
back = new Button |
|
{ |
|
Text = "<", |
|
VerticalOptions = LayoutOptions.Center, |
|
HorizontalOptions = LayoutOptions.Center, |
|
}; |
|
main.Children.Add(back, 0, 0); |
|
|
|
title = new Label |
|
{ |
|
VerticalOptions = LayoutOptions.Center, |
|
HorizontalOptions = LayoutOptions.Center, |
|
FontAttributes = FontAttributes.Bold |
|
}; |
|
main.Children.Add(title, 0, 0); |
|
Grid.SetColumnSpan(title, 3); |
|
|
|
menuItemView = new StackLayout |
|
{ |
|
Orientation = StackOrientation.Horizontal, |
|
VerticalOptions = LayoutOptions.Center, |
|
HorizontalOptions = LayoutOptions.End |
|
}; |
|
main.Children.Add(menuItemView, 2, 0); |
|
|
|
Content = main; |
|
} |
|
|
|
public static readonly BindableProperty HasBackButtonProperty = |
|
BindableProperty.Create( |
|
propertyName: nameof(HasBackButton), |
|
returnType: typeof(bool), |
|
declaringType: typeof(NavigationBar), |
|
defaultValue: false, |
|
propertyChanged: (bindable, oldValue, newValue) => |
|
{ |
|
var bar = (NavigationBar)bindable; |
|
bar.back.IsVisible = (bool)newValue; |
|
}); |
|
|
|
public bool HasBackButton |
|
{ |
|
get { return (bool)GetValue(HasBackButtonProperty); } |
|
set { SetValue(HasBackButtonProperty, value); } |
|
} |
|
|
|
public static readonly BindableProperty BackButtonTitleProperty = |
|
BindableProperty.Create( |
|
propertyName: nameof(BackButtonTitle), |
|
returnType: typeof(string), |
|
declaringType: typeof(NavigationBar), |
|
defaultValue: "<", |
|
propertyChanged: (bindable, oldValue, newValue) => |
|
{ |
|
var bar = (NavigationBar)bindable; |
|
bar.back.Text = newValue?.ToString(); |
|
}); |
|
|
|
public string BackButtonTitle |
|
{ |
|
get { return (string)GetValue(BackButtonTitleProperty); } |
|
set { SetValue(BackButtonTitleProperty, value); } |
|
} |
|
|
|
public static readonly BindableProperty TitleProperty = |
|
BindableProperty.Create( |
|
propertyName: nameof(Title), |
|
returnType: typeof(string), |
|
declaringType: typeof(NavigationBar), |
|
defaultValue: "", |
|
propertyChanged: (bindable, oldValue, newValue) => |
|
{ |
|
var bar = (NavigationBar)bindable; |
|
bar.title.Text = newValue?.ToString(); |
|
}); |
|
|
|
public string Title |
|
{ |
|
get { return (string)GetValue(TitleProperty); } |
|
set { SetValue(TitleProperty, value); } |
|
} |
|
|
|
public static readonly BindableProperty ToolbarItemsProperty = |
|
BindableProperty.Create( |
|
propertyName: nameof(ToolbarItems), |
|
returnType: typeof(IList<ToolbarItem>), |
|
declaringType: typeof(NavigationBar), |
|
propertyChanged: (bindable, oldValue, newValue) => |
|
{ |
|
var bar = (NavigationBar)bindable; |
|
bar.menuItemView.Children.Clear(); |
|
|
|
var newItems = newValue as ObservableCollection<ToolbarItem>; |
|
if (newItems != null) |
|
{ |
|
newItems.CollectionChanged += bar.OnToolbarItemsCollectionChanged; |
|
} |
|
|
|
var oldItems = oldValue as ObservableCollection<ToolbarItem>; |
|
if (oldItems != null) |
|
{ |
|
oldItems.CollectionChanged -= bar.OnToolbarItemsCollectionChanged; |
|
} |
|
}); |
|
|
|
public IList<ToolbarItem> ToolbarItems |
|
{ |
|
get { return (IList<ToolbarItem>)GetValue(ToolbarItemsProperty); } |
|
set { SetValue(ToolbarItemsProperty, value); } |
|
} |
|
|
|
private void OnToolbarItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
|
{ |
|
foreach (ToolbarItem item in e.NewItems) |
|
{ |
|
var button = new Button(); |
|
button.SetBinding(Button.ImageProperty, new Binding(ToolbarItem.IconProperty.PropertyName, BindingMode.OneWay, source: item)); |
|
button.SetBinding(Button.TextProperty, new Binding(ToolbarItem.TextProperty.PropertyName, BindingMode.OneWay, source: item)); |
|
button.SetBinding(Button.CommandProperty, new Binding(ToolbarItem.CommandProperty.PropertyName, BindingMode.OneWay, source: item)); |
|
|
|
menuItemView.Children.Add(button); |
|
} |
|
} |
|
} |
|
} |