Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@smallgeek
Created May 18, 2017 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smallgeek/7fa99002e90b343c8fb21dbd509e1aff to your computer and use it in GitHub Desktop.
Save smallgeek/7fa99002e90b343c8fb21dbd509e1aff to your computer and use it in GitHub Desktop.
MasterDetail でもメニューバーを表示するためのテンプレート試作
<?xml version="1.0" encoding="utf-8" ?>
<prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
x:Class="Smallgeek.App">
<Application.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="NavigationMenuPageTemplate">
<Grid RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<c:NavigationBar Grid.Row="0" Grid.Column="0"
Title="{TemplateBinding Title}"
ToolbarItems="{TemplateBinding ToolbarItems}"
BackCommand="GoBack"
HeightRequest="56">
<c:NavigationBar.IsVisible>
<OnIdiom x:TypeArguments="x:Boolean">
<OnIdiom.Phone>False</OnIdiom.Phone>
<OnIdiom.Tablet>True</OnIdiom.Tablet>
</OnIdiom>
</c:NavigationBar.IsVisible>
</c:NavigationBar>
<ContentPresenter Grid.Row="1" Grid.Column="0" />
</Grid>
</ControlTemplate>
</ResourceDictionary>
</Application.Resources>
</prism:PrismApplication>
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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment