Skip to content

Commit 3381011

Browse files
Merge pull request #4320 from XAML-Knight/dev/ViewportLogging
Add Property panel for ViewportBehavior
2 parents 72ab567 + 74021fb commit 3381011

File tree

3 files changed

+109
-81
lines changed

3 files changed

+109
-81
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ViewportBehavior/ViewportBehaviorXaml.bind

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Height="200"
1515
Background="Gray">
1616
<interactivity:Interaction.Behaviors>
17-
<behaviors:ViewportBehavior x:Name="ViewportBehavior" />
17+
<behaviors:ViewportBehavior x:Name="ViewportBehavior" IsAlwaysOn="True" />
1818
</interactivity:Interaction.Behaviors>
1919
<Image x:Name="EffectElement"
2020
Width="100"
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 System;
6+
using Microsoft.Xaml.Interactivity;
7+
using Windows.UI.Xaml;
8+
9+
namespace Microsoft.Toolkit.Uwp.UI.Behaviors
10+
{
11+
/// <summary>
12+
/// A class for listening to an element enter or exit the ScrollViewer viewport
13+
/// </summary>
14+
public partial class ViewportBehavior
15+
{
16+
/// <summary>
17+
/// The IsFullyInViewport value of the associated element
18+
/// </summary>
19+
public static readonly DependencyProperty IsFullyInViewportProperty =
20+
DependencyProperty.Register(nameof(IsFullyInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsFullyInViewportChanged));
21+
22+
/// <summary>
23+
/// The IsInViewport value of the associated element
24+
/// </summary>
25+
public static readonly DependencyProperty IsInViewportProperty =
26+
DependencyProperty.Register(nameof(IsInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsInViewportChanged));
27+
28+
/// <summary>
29+
/// The IsAlwaysOn value of the associated element
30+
/// </summary>
31+
public static readonly DependencyProperty IsAlwaysOnProperty =
32+
DependencyProperty.Register(nameof(IsAlwaysOn), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool)));
33+
34+
/// <summary>
35+
/// 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.
36+
/// </summary>
37+
public bool IsAlwaysOn
38+
{
39+
get { return (bool)GetValue(IsAlwaysOnProperty); }
40+
set { SetValue(IsAlwaysOnProperty, value); }
41+
}
42+
43+
/// <summary>
44+
/// Gets a value indicating whether associated element is fully in the ScrollViewer viewport
45+
/// </summary>
46+
public bool IsFullyInViewport
47+
{
48+
get { return (bool)GetValue(IsFullyInViewportProperty); }
49+
private set { SetValue(IsFullyInViewportProperty, value); }
50+
}
51+
52+
/// <summary>
53+
/// Gets a value indicating whether associated element is in the ScrollViewer viewport
54+
/// </summary>
55+
public bool IsInViewport
56+
{
57+
get { return (bool)GetValue(IsInViewportProperty); }
58+
private set { SetValue(IsInViewportProperty, value); }
59+
}
60+
61+
/// <summary>
62+
/// Event tracking when the object is fully within the viewport or not
63+
/// </summary>
64+
/// <param name="d">DependencyObject</param>
65+
/// <param name="e">EventArgs</param>
66+
private static void OnIsFullyInViewportChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
67+
{
68+
var obj = (ViewportBehavior)d;
69+
var value = (bool)e.NewValue;
70+
71+
if (value)
72+
{
73+
obj.EnteredViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty);
74+
75+
if (!obj.IsAlwaysOn)
76+
{
77+
Interaction.GetBehaviors(obj.AssociatedObject).Remove(obj);
78+
}
79+
}
80+
else
81+
{
82+
obj.ExitingViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty);
83+
}
84+
}
85+
86+
/// <summary>
87+
/// Event tracking the state of the object as it moves into and out of the viewport
88+
/// </summary>
89+
/// <param name="d">DependencyObject</param>
90+
/// <param name="e">EventArgs</param>
91+
private static void OnIsInViewportChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
92+
{
93+
var obj = (ViewportBehavior)d;
94+
var value = (bool)e.NewValue;
95+
96+
if (value)
97+
{
98+
obj.EnteringViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty);
99+
}
100+
else
101+
{
102+
obj.ExitedViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty);
103+
}
104+
}
105+
}
106+
}

Microsoft.Toolkit.Uwp.UI.Behaviors/Viewport/ViewportBehavior.cs

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,15 @@
1212
namespace Microsoft.Toolkit.Uwp.UI.Behaviors
1313
{
1414
/// <summary>
15-
/// A class for listening element enter or exit the ScrollViewer viewport
15+
/// A class for listening to an element enter or exit the ScrollViewer viewport
1616
/// </summary>
17-
public class ViewportBehavior : BehaviorBase<FrameworkElement>
17+
public partial class ViewportBehavior : BehaviorBase<FrameworkElement>
1818
{
1919
/// <summary>
2020
/// The ScrollViewer hosting this element.
2121
/// </summary>
2222
private ScrollViewer _hostScrollViewer;
2323

24-
/// <summary>
25-
/// The IsFullyInViewport value of the associated element
26-
/// </summary>
27-
public static readonly DependencyProperty IsFullyInViewportProperty =
28-
DependencyProperty.Register(nameof(IsFullyInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsFullyInViewportChanged));
29-
30-
/// <summary>
31-
/// The IsInViewport value of the associated element
32-
/// </summary>
33-
public static readonly DependencyProperty IsInViewportProperty =
34-
DependencyProperty.Register(nameof(IsInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsInViewportChanged));
35-
36-
/// <summary>
37-
/// The IsAlwaysOn value of the associated element
38-
/// </summary>
39-
public static readonly DependencyProperty IsAlwaysOnProperty =
40-
DependencyProperty.Register(nameof(IsAlwaysOn), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool)));
41-
4224
/// <summary>
4325
/// Associated element fully enter the ScrollViewer viewport event
4426
/// </summary>
@@ -59,33 +41,6 @@ public class ViewportBehavior : BehaviorBase<FrameworkElement>
5941
/// </summary>
6042
public event EventHandler ExitingViewport;
6143

62-
/// <summary>
63-
/// 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.
64-
/// </summary>
65-
public bool IsAlwaysOn
66-
{
67-
get { return (bool)GetValue(IsAlwaysOnProperty); }
68-
set { SetValue(IsAlwaysOnProperty, value); }
69-
}
70-
71-
/// <summary>
72-
/// Gets a value indicating whether associated element is fully in the ScrollViewer viewport
73-
/// </summary>
74-
public bool IsFullyInViewport
75-
{
76-
get { return (bool)GetValue(IsFullyInViewportProperty); }
77-
private set { SetValue(IsFullyInViewportProperty, value); }
78-
}
79-
80-
/// <summary>
81-
/// Gets a value indicating whether associated element is in the ScrollViewer viewport
82-
/// </summary>
83-
public bool IsInViewport
84-
{
85-
get { return (bool)GetValue(IsInViewportProperty); }
86-
private set { SetValue(IsInViewportProperty, value); }
87-
}
88-
8944
/// <summary>
9045
/// Called after the behavior is attached to the <see cref="P:Microsoft.Xaml.Interactivity.Behavior.AssociatedObject" />.
9146
/// </summary>
@@ -120,39 +75,6 @@ protected override void OnDetaching()
12075
_hostScrollViewer = null;
12176
}
12277

123-
private static void OnIsFullyInViewportChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
124-
{
125-
var obj = (ViewportBehavior)d;
126-
var value = (bool)e.NewValue;
127-
if (value)
128-
{
129-
obj.EnteredViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty);
130-
131-
if (!obj.IsAlwaysOn)
132-
{
133-
Interaction.GetBehaviors(obj.AssociatedObject).Remove(obj);
134-
}
135-
}
136-
else
137-
{
138-
obj.ExitingViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty);
139-
}
140-
}
141-
142-
private static void OnIsInViewportChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
143-
{
144-
var obj = (ViewportBehavior)d;
145-
var value = (bool)e.NewValue;
146-
if (value)
147-
{
148-
obj.EnteringViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty);
149-
}
150-
else
151-
{
152-
obj.ExitedViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty);
153-
}
154-
}
155-
15678
private void ParentScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
15779
{
15880
var associatedElementRect = AssociatedObject.TransformToVisual(_hostScrollViewer)

0 commit comments

Comments
 (0)