1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+ // See the LICENSE file in the project root for more information.
4+
5+ using Microsoft . Xaml . Interactivity ;
16using System ;
27using Windows . UI . Xaml ;
38
@@ -21,7 +26,7 @@ public partial class ViewportBehavior
2126 /// The IsAlwaysOn value of the associated element
2227 /// </summary>
2328 public static readonly DependencyProperty IsAlwaysOnProperty =
24- DependencyProperty . Register ( nameof ( IsAlwaysOn ) , typeof ( bool ) , typeof ( ViewportBehavior ) , new PropertyMetadata ( true ) ) ;
29+ DependencyProperty . Register ( nameof ( IsAlwaysOn ) , typeof ( bool ) , typeof ( ViewportBehavior ) , new PropertyMetadata ( default ( bool ) ) ) ;
2530
2631 /// <summary>
2732 /// 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.
@@ -60,17 +65,19 @@ private static void OnIsFullyInViewportChanged(DependencyObject d, DependencyPro
6065 var obj = ( ViewportBehavior ) d ;
6166 var value = ( bool ) e . NewValue ;
6267
63- if ( obj . IsAlwaysOn )
68+ if ( value )
6469 {
65- if ( value )
66- {
67- obj . EnteredViewport ? . Invoke ( obj . AssociatedObject , EventArgs . Empty ) ;
68- }
69- else
70+ obj . EnteredViewport ? . Invoke ( obj . AssociatedObject , EventArgs . Empty ) ;
71+
72+ if ( ! obj . IsAlwaysOn )
7073 {
71- obj . ExitingViewport ? . Invoke ( obj . AssociatedObject , EventArgs . Empty ) ;
74+ Interaction . GetBehaviors ( obj . AssociatedObject ) . Remove ( obj ) ;
7275 }
7376 }
77+ else
78+ {
79+ obj . ExitingViewport ? . Invoke ( obj . AssociatedObject , EventArgs . Empty ) ;
80+ }
7481 }
7582
7683 /// <summary>
@@ -83,16 +90,13 @@ private static void OnIsInViewportChanged(DependencyObject d, DependencyProperty
8390 var obj = ( ViewportBehavior ) d ;
8491 var value = ( bool ) e . NewValue ;
8592
86- if ( obj . IsAlwaysOn )
93+ if ( value )
8794 {
88- if ( value )
89- {
90- obj . EnteringViewport ? . Invoke ( obj . AssociatedObject , EventArgs . Empty ) ;
91- }
92- else
93- {
94- obj . ExitedViewport ? . Invoke ( obj . AssociatedObject , EventArgs . Empty ) ;
95- }
95+ obj . EnteringViewport ? . Invoke ( obj . AssociatedObject , EventArgs . Empty ) ;
96+ }
97+ else
98+ {
99+ obj . ExitedViewport ? . Invoke ( obj . AssociatedObject , EventArgs . Empty ) ;
96100 }
97101 }
98102 }
0 commit comments