using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.Xaml;
namespace WindowsStateTriggers
{
///
/// Extends the functionality with
/// interface implementation
/// for usage
///
public class AdaptiveTrigger : Windows.UI.Xaml.AdaptiveTrigger, ITriggerValue
{
///
/// Initializes a new instance of the class.
///
public AdaptiveTrigger()
{
this.RegisterPropertyChangedCallback(MinWindowHeightProperty, OnMinWindowHeightPropertyChanged);
this.RegisterPropertyChangedCallback(MinWindowWidthProperty, OnMinWindowWidthPropertyChanged);
var window = CoreApplication.GetCurrentView()?.CoreWindow;
if (window != null)
{
var weakEvent = new WeakEventListener(this)
{
OnEventAction = (instance, s, e) => OnCoreWindowOnSizeChanged(s, e),
OnDetachAction = (instance, weakEventListener) => window.SizeChanged -= weakEventListener.OnEvent
};
window.SizeChanged += weakEvent.OnEvent;
}
}
private void OnCoreWindowOnSizeChanged(CoreWindow sender, WindowSizeChangedEventArgs args)
{
IsActive = args.Size.Height >= MinWindowHeight && args.Size.Width >= MinWindowWidth;
}
private void OnMinWindowHeightPropertyChanged(DependencyObject sender, DependencyProperty dp)
{
var window = CoreApplication.GetCurrentView()?.CoreWindow;
if (window != null)
{
IsActive = window.Bounds.Height >= MinWindowHeight;
}
}
private void OnMinWindowWidthPropertyChanged(DependencyObject sender, DependencyProperty dp)
{
var window = CoreApplication.GetCurrentView()?.CoreWindow;
if (window != null)
{
IsActive = window.Bounds.Width >= MinWindowWidth;
}
}
#region ITriggerValue
private bool _isActive;
///
/// Gets a value indicating whether this trigger is active.
///
/// true if this trigger is active; otherwise, false.
public bool IsActive
{
get { return _isActive; }
private set
{
if (_isActive != value)
{
_isActive = value;
IsActiveChanged?.Invoke(this, EventArgs.Empty);
}
}
}
///
/// Occurs when the property has changed.
///
public event EventHandler IsActiveChanged;
#endregion ITriggerValue
}
}