Created
March 16, 2014 18:16
-
-
Save ScottIsAFool/9587465 to your computer and use it in GitHub Desktop.
Hide appbar when using SlideView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows; | |
using Microsoft.Phone.Controls; | |
using System.Windows.Controls; | |
namespace SlideView.Library | |
{ | |
[TemplatePart(Name = SlideViewName, Type = typeof(SlideView))] | |
public class SlideApplicationFrame : TransitionFrame | |
{ | |
internal const string SlideViewName = "MainPanel"; | |
internal const string LeftPanelButtonName = "LeftPanelButton"; | |
internal const string RightPanelButtonName = "RightPanelButton"; | |
internal const string HeaderGridName = "HeaderGrid"; | |
private const int IndexLeftPanel = 0; | |
private const int IndexCenterPanel = 1; | |
private const int IndexRightPanel = 2; | |
#region LeftContent (DependencyProperty) | |
public object LeftContent | |
{ | |
get { return (object)GetValue(LeftContentProperty); } | |
set { SetValue(LeftContentProperty, value); } | |
} | |
public static readonly DependencyProperty LeftContentProperty = | |
DependencyProperty.Register("LeftContent", typeof(object), typeof(SlideApplicationFrame), | |
new PropertyMetadata(null)); | |
#endregion | |
#region RightContent (DependencyProperty) | |
public object RightContent | |
{ | |
get { return (object)GetValue(RightContentProperty); } | |
set { SetValue(RightContentProperty, value); } | |
} | |
public static readonly DependencyProperty RightContentProperty = | |
DependencyProperty.Register("RightContent", typeof(object), typeof(SlideApplicationFrame), | |
new PropertyMetadata(null)); | |
#endregion | |
#region Title (DependencyProperty) | |
/// <summary> | |
/// App title | |
/// </summary> | |
public string Title | |
{ | |
get { return (string)GetValue(TitleProperty); } | |
set { SetValue(TitleProperty, value); } | |
} | |
public static readonly DependencyProperty TitleProperty = | |
DependencyProperty.Register("Title", typeof(string), typeof(SlideApplicationFrame), | |
new PropertyMetadata(null)); | |
#endregion | |
#region IsHeaderVisible (DependencyProperty) | |
public static readonly DependencyProperty IsHeaderVisibleProperty = | |
DependencyProperty.Register("IsHeaderVisible", typeof(bool), typeof(SlideApplicationFrame), new PropertyMetadata(true, IsHeaderVisibleChanged)); | |
public bool IsHeaderVisible | |
{ | |
get { return (bool)GetValue(IsHeaderVisibleProperty); } | |
set { SetValue(IsHeaderVisibleProperty, value); } | |
} | |
private static void IsHeaderVisibleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) | |
{ | |
var slider = sender as SlideApplicationFrame; | |
if (slider == null) | |
{ | |
return; | |
} | |
if (slider._headerGrid == null) | |
{ | |
return; | |
} | |
slider._headerGrid.Visibility = slider.IsHeaderVisible ? Visibility.Visible : Visibility.Collapsed; | |
} | |
#endregion | |
#region IsSlideEnabled (DependencyProperty) | |
/// <summary> | |
/// If true, enables the slide interaction between panels, otherwise disables it | |
/// </summary> | |
public bool IsSlideEnabled | |
{ | |
get { return (bool)GetValue(IsSlideEnabledProperty); } | |
set { SetValue(IsSlideEnabledProperty, value); } | |
} | |
public static readonly DependencyProperty IsSlideEnabledProperty = | |
DependencyProperty.Register("IsSlideEnabled", typeof(bool), typeof(SlideApplicationFrame), | |
new PropertyMetadata(true, OnIsSlideEnabledChanged)); | |
private static void OnIsSlideEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
((SlideApplicationFrame)d).OnIsSlideEnabledChanged(); | |
} | |
protected virtual void OnIsSlideEnabledChanged() | |
{ | |
if (_mainPanel != null) | |
_mainPanel.IsSlideEnabled = IsSlideEnabled; | |
if (_leftButton != null) | |
_leftButton.Visibility = IsSlideEnabled ? Visibility.Visible : Visibility.Collapsed; | |
if (_rightButton != null) | |
_rightButton.Visibility = IsSlideEnabled ? Visibility.Visible : Visibility.Collapsed; | |
} | |
#endregion | |
#region HideAppBarWhenOffCentre (DependencyProperty) | |
public static readonly DependencyProperty HideAppBarWhenOffCentreProperty = DependencyProperty.Register( | |
"HideAppBarWhenOffCentre", typeof(bool), typeof(SlideApplicationFrame), new PropertyMetadata(default(bool))); | |
public bool HideAppBarWhenOffCentre | |
{ | |
get { return (bool)GetValue(HideAppBarWhenOffCentreProperty); } | |
set { SetValue(HideAppBarWhenOffCentreProperty, value); } | |
} | |
#endregion | |
public static readonly DependencyProperty ContentVisibleProperty = | |
DependencyProperty.Register("ContentVisible", typeof(ContentVisible), typeof(SlideApplicationFrame), new PropertyMetadata(default(ContentVisible))); | |
public ContentVisible ContentVisible | |
{ | |
get { return (ContentVisible)GetValue(ContentVisibleProperty); } | |
set { SetValue(ContentVisibleProperty, value); } | |
} | |
private SlideView _mainPanel; | |
private Button _leftButton; | |
private Button _rightButton; | |
private Grid _headerGrid; | |
public SlideApplicationFrame() | |
{ | |
DefaultStyleKey = typeof(SlideApplicationFrame); | |
Navigating += SlideApplicationFrame_Navigating; | |
BackKeyPress += SlideApplicationFrame_BackKeyPress; | |
} | |
void SlideApplicationFrame_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e) | |
{ | |
// Every time we navigate, we animate to the center panel | |
if (_mainPanel != null) | |
_mainPanel.SelectedIndex = IndexCenterPanel; | |
} | |
void SlideApplicationFrame_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e) | |
{ | |
// Pressing back displays the center panel | |
if (ContentVisible != ContentVisible.Center) | |
{ | |
e.Cancel = true; | |
ShowCenterContent(); | |
} | |
} | |
public override void OnApplyTemplate() | |
{ | |
base.OnApplyTemplate(); | |
_mainPanel = GetTemplateChild(SlideViewName) as SlideView; | |
if (_mainPanel != null) | |
_mainPanel.SelectionChanged += MainPanelOnSelectionChanged; | |
_leftButton = GetTemplateChild(LeftPanelButtonName) as Button; | |
if (_leftButton != null) | |
_leftButton.Click += LeftPanelButton_Click; | |
_rightButton = GetTemplateChild(RightPanelButtonName) as Button; | |
if (_rightButton != null) | |
_rightButton.Click += RightPanelButton_Click; | |
_headerGrid = GetTemplateChild(HeaderGridName) as Grid; | |
if (_headerGrid != null) | |
_headerGrid.Visibility = IsHeaderVisible ? Visibility.Visible : Visibility.Collapsed; | |
OnIsSlideEnabledChanged(); | |
} | |
public void ShowCenterContent() | |
{ | |
if (_mainPanel != null) | |
{ | |
_mainPanel.SelectedIndex = 1; | |
} | |
} | |
private void MainPanelOnSelectionChanged(object sender, EventArgs eventArgs) | |
{ | |
if (_mainPanel == null) return; | |
switch (_mainPanel.SelectedIndex) | |
{ | |
case 0: | |
ContentVisible = ContentVisible.Left; | |
ShowHideApplicationBar(false); | |
break; | |
case 1: | |
ContentVisible = ContentVisible.Center; | |
ShowHideApplicationBar(true); | |
break; | |
case 2: | |
ContentVisible = ContentVisible.Right; | |
ShowHideApplicationBar(false); | |
break; | |
} | |
} | |
private void ShowHideApplicationBar(bool isCentre) | |
{ | |
if (!HideAppBarWhenOffCentre) return; | |
var page = Content as PhoneApplicationPage; | |
if (page != null && page.ApplicationBar != null) | |
{ | |
page.ApplicationBar.IsVisible = isCentre; | |
} | |
} | |
void RightPanelButton_Click(object sender, RoutedEventArgs e) | |
{ | |
if (_mainPanel.SelectedIndex != IndexRightPanel) | |
_mainPanel.SelectedIndex = IndexRightPanel; | |
else | |
_mainPanel.SelectedIndex = IndexCenterPanel; | |
} | |
void LeftPanelButton_Click(object sender, RoutedEventArgs e) | |
{ | |
if (_mainPanel.SelectedIndex != IndexLeftPanel) | |
_mainPanel.SelectedIndex = IndexLeftPanel; | |
else | |
_mainPanel.SelectedIndex = IndexCenterPanel; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment