Moved code into *.Properties.cs file, wired up IsAlwaysOn flag
This commit is contained in:
Родитель
d766b9ee59
Коммит
7ef191888a
|
@ -7,7 +7,7 @@
|
||||||
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
|
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
|
||||||
mc:Ignorable="d">
|
mc:Ignorable="d">
|
||||||
<Page.Resources>
|
<Page.Resources>
|
||||||
<behaviors:ViewportBehavior x:Key="ViewportBehavior" IsAlwaysOn="True"/>
|
<behaviors:ViewportBehavior x:Key="ViewportBehavior" />
|
||||||
</Page.Resources>
|
</Page.Resources>
|
||||||
|
|
||||||
<Grid>
|
<Grid>
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
Height="200"
|
Height="200"
|
||||||
Background="Gray">
|
Background="Gray">
|
||||||
<interactivity:Interaction.Behaviors>
|
<interactivity:Interaction.Behaviors>
|
||||||
<behaviors:ViewportBehavior x:Name="ViewportBehavior" />
|
<behaviors:ViewportBehavior x:Name="ViewportBehavior" IsAlwaysOn="@[IsAlwaysOn:Bool:True]" />
|
||||||
</interactivity:Interaction.Behaviors>
|
</interactivity:Interaction.Behaviors>
|
||||||
<Image x:Name="EffectElement"
|
<Image x:Name="EffectElement"
|
||||||
Width="100"
|
Width="100"
|
||||||
|
|
|
@ -1,9 +1,25 @@
|
||||||
|
using System;
|
||||||
using Windows.UI.Xaml;
|
using Windows.UI.Xaml;
|
||||||
|
|
||||||
namespace Microsoft.Toolkit.Uwp.UI.Behaviors
|
namespace Microsoft.Toolkit.Uwp.UI.Behaviors
|
||||||
{
|
{
|
||||||
public partial class ViewportBehavior
|
public partial class ViewportBehavior
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The IsFullyInViewport value of the associated element
|
||||||
|
/// </summary>
|
||||||
|
public static readonly DependencyProperty IsFullyInViewportProperty =
|
||||||
|
DependencyProperty.Register(nameof(IsFullyInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsFullyInViewportChanged));
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The IsInViewport value of the associated element
|
||||||
|
/// </summary>
|
||||||
|
public static readonly DependencyProperty IsInViewportProperty =
|
||||||
|
DependencyProperty.Register(nameof(IsInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsInViewportChanged));
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The IsAlwaysOn value of the associated element
|
||||||
|
/// </summary>
|
||||||
public static readonly DependencyProperty IsAlwaysOnProperty =
|
public static readonly DependencyProperty IsAlwaysOnProperty =
|
||||||
DependencyProperty.Register(nameof(IsAlwaysOn), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(true));
|
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); }
|
get { return (bool)GetValue(IsAlwaysOnProperty); }
|
||||||
set { SetValue(IsAlwaysOnProperty, value); }
|
set { SetValue(IsAlwaysOnProperty, value); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether associated element is fully in the ScrollViewer viewport
|
||||||
|
/// </summary>
|
||||||
|
public bool IsFullyInViewport
|
||||||
|
{
|
||||||
|
get { return (bool)GetValue(IsFullyInViewportProperty); }
|
||||||
|
private set { SetValue(IsFullyInViewportProperty, value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether associated element is in the ScrollViewer viewport
|
||||||
|
/// </summary>
|
||||||
|
public bool IsInViewport
|
||||||
|
{
|
||||||
|
get { return (bool)GetValue(IsInViewportProperty); }
|
||||||
|
private set { SetValue(IsInViewportProperty, value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Event tracking when the object is fully within the viewport or not
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="d">ViewportBehavior</param>
|
||||||
|
/// <param name="e">EventArgs</param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Event tracking the state of the object as it moves in and out of the viewport
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="d">ViewportBehavior</param>
|
||||||
|
/// <param name="e">EventArgs</param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,24 +21,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Behaviors
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private ScrollViewer _hostScrollViewer;
|
private ScrollViewer _hostScrollViewer;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The IsFullyInViewport value of the associated element
|
|
||||||
/// </summary>
|
|
||||||
public static readonly DependencyProperty IsFullyInViewportProperty =
|
|
||||||
DependencyProperty.Register(nameof(IsFullyInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsFullyInViewportChanged));
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The IsInViewport value of the associated element
|
|
||||||
/// </summary>
|
|
||||||
public static readonly DependencyProperty IsInViewportProperty =
|
|
||||||
DependencyProperty.Register(nameof(IsInViewport), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(default(bool), OnIsInViewportChanged));
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The IsAlwaysOn value of the associated element
|
|
||||||
/// </summary>
|
|
||||||
//public static readonly DependencyProperty IsAlwaysOnProperty =
|
|
||||||
// DependencyProperty.Register(nameof(IsAlwaysOn), typeof(bool), typeof(ViewportBehavior), new PropertyMetadata(true));
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Associated element fully enter the ScrollViewer viewport event
|
/// Associated element fully enter the ScrollViewer viewport event
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -59,33 +41,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Behaviors
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event EventHandler ExitingViewport;
|
public event EventHandler ExitingViewport;
|
||||||
|
|
||||||
///// <summary>
|
|
||||||
///// 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.
|
|
||||||
///// </summary>
|
|
||||||
//public bool IsAlwaysOn
|
|
||||||
//{
|
|
||||||
// get { return (bool)GetValue(IsAlwaysOnProperty); }
|
|
||||||
// set { SetValue(IsAlwaysOnProperty, value); }
|
|
||||||
//}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether associated element is fully in the ScrollViewer viewport
|
|
||||||
/// </summary>
|
|
||||||
public bool IsFullyInViewport
|
|
||||||
{
|
|
||||||
get { return (bool)GetValue(IsFullyInViewportProperty); }
|
|
||||||
private set { SetValue(IsFullyInViewportProperty, value); }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether associated element is in the ScrollViewer viewport
|
|
||||||
/// </summary>
|
|
||||||
public bool IsInViewport
|
|
||||||
{
|
|
||||||
get { return (bool)GetValue(IsInViewportProperty); }
|
|
||||||
private set { SetValue(IsInViewportProperty, value); }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called after the behavior is attached to the <see cref="P:Microsoft.Xaml.Interactivity.Behavior.AssociatedObject" />.
|
/// Called after the behavior is attached to the <see cref="P:Microsoft.Xaml.Interactivity.Behavior.AssociatedObject" />.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -120,39 +75,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Behaviors
|
||||||
_hostScrollViewer = null;
|
_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)
|
private void ParentScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
|
||||||
{
|
{
|
||||||
var associatedElementRect = AssociatedObject.TransformToVisual(_hostScrollViewer)
|
var associatedElementRect = AssociatedObject.TransformToVisual(_hostScrollViewer)
|
||||||
|
|
Загрузка…
Ссылка в новой задаче