From 7ef191888a9c67066b02fb27eeee84b640aecc89 Mon Sep 17 00:00:00 2001 From: Darren Batchelor Date: Wed, 13 Oct 2021 16:35:37 -0700 Subject: [PATCH] Moved code into *.Properties.cs file, wired up IsAlwaysOn flag --- .../ViewportBehaviorPage.xaml | 2 +- .../ViewportBehaviorXaml.bind | 2 +- .../Viewport/ViewportBehavior.Properties.cs | 80 +++++++++++++++++++ .../Viewport/ViewportBehavior.cs | 78 ------------------ 4 files changed, 82 insertions(+), 80 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ViewportBehavior/ViewportBehaviorPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ViewportBehavior/ViewportBehaviorPage.xaml index 510d20583..6db0bbda0 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ViewportBehavior/ViewportBehaviorPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ViewportBehavior/ViewportBehaviorPage.xaml @@ -7,7 +7,7 @@ xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors" mc:Ignorable="d"> - + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ViewportBehavior/ViewportBehaviorXaml.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ViewportBehavior/ViewportBehaviorXaml.bind index e749fca1e..a50ae1088 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ViewportBehavior/ViewportBehaviorXaml.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ViewportBehavior/ViewportBehaviorXaml.bind @@ -14,7 +14,7 @@ Height="200" Background="Gray"> - + + /// The IsFullyInViewport value of the associated element + /// + public static readonly DependencyProperty IsFullyInViewportProperty = + DependencyProperty.Register(nameof(IsFullyInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsFullyInViewportChanged)); + + /// + /// The IsInViewport value of the associated element + /// + public static readonly DependencyProperty IsInViewportProperty = + DependencyProperty.Register(nameof(IsInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsInViewportChanged)); + + /// + /// The IsAlwaysOn value of the associated element + /// public static readonly DependencyProperty IsAlwaysOnProperty = DependencyProperty.Register(nameof(IsAlwaysOn), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(true)); @@ -15,5 +31,69 @@ namespace Microsoft.Toolkit.Uwp.UI.Behaviors get { return (bool)GetValue(IsAlwaysOnProperty); } set { SetValue(IsAlwaysOnProperty, value); } } + + /// + /// Gets a value indicating whether associated element is fully in the ScrollViewer viewport + /// + public bool IsFullyInViewport + { + get { return (bool)GetValue(IsFullyInViewportProperty); } + private set { SetValue(IsFullyInViewportProperty, value); } + } + + /// + /// Gets a value indicating whether associated element is in the ScrollViewer viewport + /// + public bool IsInViewport + { + get { return (bool)GetValue(IsInViewportProperty); } + private set { SetValue(IsInViewportProperty, value); } + } + + /// + /// Event tracking when the object is fully within the viewport or not + /// + /// ViewportBehavior + /// EventArgs + private static void OnIsFullyInViewportChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var obj = (ViewportBehavior)d; + var value = (bool)e.NewValue; + + if (obj.IsAlwaysOn) + { + if (value) + { + obj.EnteredViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty); + } + else + { + obj.ExitingViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty); + } + } + } + + /// + /// Event tracking the state of the object as it moves in and out of the viewport + /// + /// ViewportBehavior + /// EventArgs + private static void OnIsInViewportChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var obj = (ViewportBehavior)d; + var value = (bool)e.NewValue; + + if (obj.IsAlwaysOn) + { + if (value) + { + obj.EnteringViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty); + } + else + { + obj.ExitedViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty); + } + } + } } } diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/Viewport/ViewportBehavior.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/Viewport/ViewportBehavior.cs index cc82e9788..306891ffc 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/Viewport/ViewportBehavior.cs +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/Viewport/ViewportBehavior.cs @@ -21,24 +21,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Behaviors /// private ScrollViewer _hostScrollViewer; - /// - /// The IsFullyInViewport value of the associated element - /// - public static readonly DependencyProperty IsFullyInViewportProperty = - DependencyProperty.Register(nameof(IsFullyInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsFullyInViewportChanged)); - - /// - /// The IsInViewport value of the associated element - /// - public static readonly DependencyProperty IsInViewportProperty = - DependencyProperty.Register(nameof(IsInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsInViewportChanged)); - - /// - /// The IsAlwaysOn value of the associated element - /// - //public static readonly DependencyProperty IsAlwaysOnProperty = - // DependencyProperty.Register(nameof(IsAlwaysOn), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(true)); - /// /// Associated element fully enter the ScrollViewer viewport event /// @@ -59,33 +41,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Behaviors /// public event EventHandler ExitingViewport; - ///// - ///// Gets or sets a value indicating whether this behavior will remain attached after the associated element enters the viewport. When false, the behavior will remove itself after entering. - ///// - //public bool IsAlwaysOn - //{ - // get { return (bool)GetValue(IsAlwaysOnProperty); } - // set { SetValue(IsAlwaysOnProperty, value); } - //} - - /// - /// Gets a value indicating whether associated element is fully in the ScrollViewer viewport - /// - public bool IsFullyInViewport - { - get { return (bool)GetValue(IsFullyInViewportProperty); } - private set { SetValue(IsFullyInViewportProperty, value); } - } - - /// - /// Gets a value indicating whether associated element is in the ScrollViewer viewport - /// - public bool IsInViewport - { - get { return (bool)GetValue(IsInViewportProperty); } - private set { SetValue(IsInViewportProperty, value); } - } - /// /// Called after the behavior is attached to the . /// @@ -120,39 +75,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Behaviors _hostScrollViewer = null; } - private static void OnIsFullyInViewportChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var obj = (ViewportBehavior)d; - var value = (bool)e.NewValue; - if (value) - { - obj.EnteredViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty); - - if (!obj.IsAlwaysOn) - { - Interaction.GetBehaviors(obj.AssociatedObject).Remove(obj); - } - } - else - { - obj.ExitingViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty); - } - } - - private static void OnIsInViewportChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var obj = (ViewportBehavior)d; - var value = (bool)e.NewValue; - if (value) - { - obj.EnteringViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty); - } - else - { - obj.ExitedViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty); - } - } - private void ParentScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e) { var associatedElementRect = AssociatedObject.TransformToVisual(_hostScrollViewer)