Skip to content

Commit 7ef1918

Browse files
committed
Moved code into *.Properties.cs file, wired up IsAlwaysOn flag
1 parent d766b9e commit 7ef1918

File tree

4 files changed

+82
-80
lines changed

4 files changed

+82
-80
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ViewportBehavior/ViewportBehaviorPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
88
mc:Ignorable="d">
99
<Page.Resources>
10-
<behaviors:ViewportBehavior x:Key="ViewportBehavior" IsAlwaysOn="True"/>
10+
<behaviors:ViewportBehavior x:Key="ViewportBehavior" />
1111
</Page.Resources>
1212

1313
<Grid>

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="@[IsAlwaysOn:Bool:True]" />
1818
</interactivity:Interaction.Behaviors>
1919
<Image x:Name="EffectElement"
2020
Width="100"

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
using System;
12
using Windows.UI.Xaml;
23

34
namespace Microsoft.Toolkit.Uwp.UI.Behaviors
45
{
56
public partial class ViewportBehavior
67
{
8+
/// <summary>
9+
/// The IsFullyInViewport value of the associated element
10+
/// </summary>
11+
public static readonly DependencyProperty IsFullyInViewportProperty =
12+
DependencyProperty.Register(nameof(IsFullyInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsFullyInViewportChanged));
13+
14+
/// <summary>
15+
/// The IsInViewport value of the associated element
16+
/// </summary>
17+
public static readonly DependencyProperty IsInViewportProperty =
18+
DependencyProperty.Register(nameof(IsInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsInViewportChanged));
19+
20+
/// <summary>
21+
/// The IsAlwaysOn value of the associated element
22+
/// </summary>
723
public static readonly DependencyProperty IsAlwaysOnProperty =
824
DependencyProperty.Register(nameof(IsAlwaysOn), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(true));
925

@@ -15,5 +31,69 @@ public bool IsAlwaysOn
1531
get { return (bool)GetValue(IsAlwaysOnProperty); }
1632
set { SetValue(IsAlwaysOnProperty, value); }
1733
}
34+
35+
/// <summary>
36+
/// Gets a value indicating whether associated element is fully in the ScrollViewer viewport
37+
/// </summary>
38+
public bool IsFullyInViewport
39+
{
40+
get { return (bool)GetValue(IsFullyInViewportProperty); }
41+
private set { SetValue(IsFullyInViewportProperty, value); }
42+
}
43+
44+
/// <summary>
45+
/// Gets a value indicating whether associated element is in the ScrollViewer viewport
46+
/// </summary>
47+
public bool IsInViewport
48+
{
49+
get { return (bool)GetValue(IsInViewportProperty); }
50+
private set { SetValue(IsInViewportProperty, value); }
51+
}
52+
53+
/// <summary>
54+
/// Event tracking when the object is fully within the viewport or not
55+
/// </summary>
56+
/// <param name="d">ViewportBehavior</param>
57+
/// <param name="e">EventArgs</param>
58+
private static void OnIsFullyInViewportChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
59+
{
60+
var obj = (ViewportBehavior)d;
61+
var value = (bool)e.NewValue;
62+
63+
if (obj.IsAlwaysOn)
64+
{
65+
if (value)
66+
{
67+
obj.EnteredViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty);
68+
}
69+
else
70+
{
71+
obj.ExitingViewport?.Invoke(obj.AssociatedObject, EventArgs.Empty);
72+
}
73+
}
74+
}
75+
76+
/// <summary>
77+
/// Event tracking the state of the object as it moves in and out of the viewport
78+
/// </summary>
79+
/// <param name="d">ViewportBehavior</param>
80+
/// <param name="e">EventArgs</param>
81+
private static void OnIsInViewportChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
82+
{
83+
var obj = (ViewportBehavior)d;
84+
var value = (bool)e.NewValue;
85+
86+
if (obj.IsAlwaysOn)
87+
{
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+
}
96+
}
97+
}
1898
}
1999
}

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

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,6 @@ public partial class ViewportBehavior : BehaviorBase<FrameworkElement>
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(true));
41-
4224
/// <summary>
4325
/// Associated element fully enter the ScrollViewer viewport event
4426
/// </summary>
@@ -59,33 +41,6 @@ public partial 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)