namespace CustomTriggerSample.Triggers
{
    using System;
    using Windows.Graphics.Display;
    using Windows.UI.Xaml;

    /// <summary>
    /// The <see cref="OrientationTrigger"/> is activated when the given Orientation
    /// matches the current orientation of the device (portrait or landscape).
    /// </summary>
    public class OrientationTrigger : StateTriggerBase
    {
        public string Orientation { get; set; }

        public OrientationTrigger()
        {
            var currentDisplayInfo = DisplayInformation.GetForCurrentView();
            currentDisplayInfo.OrientationChanged += HandleDisplayOrientationChanged;
        }
        private void HandleDisplayOrientationChanged(DisplayInformation sender, object args)
        {
            var currentOrientationString = sender.CurrentOrientation.ToString();
            if (currentOrientationString != null)
            {
                // These orientations also exist but are not handled: PortraitFlipped, LandscapeFlipped
                SetActive(currentOrientationString.StartsWith(Orientation, StringComparison.CurrentCultureIgnoreCase));
            }
        }
    }
}
