Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Microsoft.Expression.Interactivity;
using Microsoft.Phone.Controls;
using System.Windows;
using System.Windows.Interactivity;
namespace KatsuYuzu.Interactivity
{
/// <summary>
/// ページの向きを基に状態を切り替えます。
/// </summary>
public sealed class OrientationStateBehavior : Behavior<PhoneApplicationPage>
{
#region PortraitUpState
/// <summary>
/// 縦長の向きの場合の状態名を取得または設定します。
/// </summary>
[CustomPropertyValueEditor(CustomPropertyValueEditor.StateName)]
public string PortraitUpState
{
get { return (string)GetValue(PortraitUpStateProperty); }
set { SetValue(PortraitUpStateProperty, value); }
}
// Using a DependencyProperty as the backing store for PortraitUpState. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PortraitUpStateProperty =
DependencyProperty.Register("PortraitUpState", typeof(string), typeof(OrientationStateBehavior), new PropertyMetadata(null));
#endregion
#region LandscapeLeftState
/// <summary>
/// ページの上部を左に回転させた横向きの場合の状態名を取得または設定します。
/// </summary>
[CustomPropertyValueEditor(CustomPropertyValueEditor.StateName)]
public string LandscapeLeftState
{
get { return (string)GetValue(LandscapeLeftStateProperty); }
set { SetValue(LandscapeLeftStateProperty, value); }
}
// Using a DependencyProperty as the backing store for LandscapeLeftState. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LandscapeLeftStateProperty =
DependencyProperty.Register("LandscapeLeftState", typeof(string), typeof(OrientationStateBehavior), new PropertyMetadata(null));
#endregion
#region LandscapeRightState
/// <summary>
/// ページの上部を右に回転させた横向きの場合の状態名を取得または設定します。
/// </summary>
[CustomPropertyValueEditor(CustomPropertyValueEditor.StateName)]
public string LandscapeRightState
{
get { return (string)GetValue(LandscapeRightStateProperty); }
set { SetValue(LandscapeRightStateProperty, value); }
}
// Using a DependencyProperty as the backing store for LandscapeRightState. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LandscapeRightStateProperty =
DependencyProperty.Register("LandscapeRightState", typeof(string), typeof(OrientationStateBehavior), new PropertyMetadata(null));
#endregion
#region UseTransitions
/// <summary>
/// System.Windows.VisualTransition を使用して状態を切り替える場合は true、それ以外は false。
/// </summary>
public bool UseTransitions
{
get { return (bool)GetValue(UseTransitionsProperty); }
set { SetValue(UseTransitionsProperty, value); }
}
// Using a DependencyProperty as the backing store for UseTransitions. This enables animation, styling, binding, etc...
public static readonly DependencyProperty UseTransitionsProperty =
DependencyProperty.Register("UseTransitions", typeof(bool), typeof(OrientationStateBehavior), new PropertyMetadata(true));
#endregion
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Loaded += this.AssociatedObject_Loaded;
this.AssociatedObject.OrientationChanged += this.AssociatedObject_OrientationChanged;
}
protected override void OnDetaching()
{
this.AssociatedObject.Loaded -= this.AssociatedObject_Loaded;
this.AssociatedObject.OrientationChanged -= this.AssociatedObject_OrientationChanged;
base.OnDetaching();
}
void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
this.GoToState(this.AssociatedObject.Orientation);
}
void AssociatedObject_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
this.GoToState(e.Orientation);
}
/// <summary>
/// 状態を切り替えます。
/// </summary>
/// <param name="orientation"></param>
void GoToState(PageOrientation orientation)
{
string stateName;
switch (orientation)
{
case PageOrientation.PortraitUp:
stateName = this.PortraitUpState;
break;
case PageOrientation.LandscapeLeft:
stateName = this.LandscapeLeftState;
break;
case PageOrientation.LandscapeRight:
stateName = this.LandscapeRightState;
break;
default:
stateName = string.Empty;
break;
}
VisualStateUtilities.GoToState(this.AssociatedObject, stateName, this.UseTransitions);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment