Behavior in Windows-Runtime Application
I have a set ScrollViewer
in my Windows Store app that should always have an action when they are double clicked (namely expand to full size). They are in different files, or I would probably just put this in the code for that file:
private void ScrollViewer_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
ScrollViewer sv = (ScrollViewer)sender;
if (sv.HorizontalScrollBarVisibility == ScrollBarVisibility.Disabled)
{
sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
}
else
{
sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
}
}
I was told to look at Behaviors, so I researched the behavior and with a few online tutorials I was able to come up with the following:
class ViewboxDoubleTap : DependencyObject, IBehavior
{
public interface IBehavior
{
DependencyObject AssociatedObject { get; }
void Attach(DependencyObject associatedObject);
void Detach();
}
public void Attach(DependencyObject associatedObject)
{
if ((associatedObject != AssociatedObject) && !DesignMode.DesignModeEnabled)
{
if (AssociatedObject != null)
throw new InvalidOperationException("Cannot attach behavior multiple times.");
AssociatedObject = associatedObject;
var fe = AssociatedObject as FrameworkElement;
if (fe != null)
{
fe.AddHandler(UIElement.DoubleTappedEvent, new DoubleTappedEventHandler(ScrollViewer_DoubleTapped), true);
}
}
}
public void Detach()
{
var fe = AssociatedObject as FrameworkElement;
if (fe != null)
{
fe.RemoveHandler(UIElement.DoubleTappedEvent, new DoubleTappedEventHandler(ScrollViewer_DoubleTapped));
}
AssociatedObject = null;
}
public DependencyObject AssociatedObject { get; private set; }
private void ScrollViewer_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
ScrollViewer sv = (ScrollViewer)sender;
if (sv.HorizontalScrollBarVisibility == ScrollBarVisibility.Disabled)
{
sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
}
else
{
sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
}
}
}
Everything compiles nicely now, but I can't bind it to XAML:
XML namespaces:
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:local="using:MyApp.Folder"
xmlns:global="using:MyApp"
Relevant code:
<ScrollViewer HorizontalAlignment="Left" VerticalScrollBarVisibility="Hidden" MaxZoomFactor="2" MinZoomFactor="1" MaxWidth="1067">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="DoubleTapped">
<global:ViewboxDoubleTap />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
<Border BorderBrush="Black" BorderThickness="1">
<Image Source="MyImage.png"/>
</Border>
</ScrollViewer>
When I try to go to this page nothing happens and when I debug it gives me this error message:
WinRT information: Cannot add instance of type 'LearnOneNote.ViewboxDoubleTap' to a collection of type 'Microsoft.Xaml.Interactivity.ActionCollection'. [Line: 25 Position: 30]
In addition, he tells me that ViewboxDoubleTap
he is not in namespace MyApp
, but when I type <global:
, he pulls it out himself; I don't know if this matters to the problem.
Any help would be appreciated.
source to share
After heading out to check out IAction
(thanks franssu) I came up with the following:
[DefaultEvent(typeof(ScrollViewer),"DoubleTapped")]
public class ScrollViewerDoubleTap : DependencyObject, IAction
{
public object Execute(object sender, object parameter)
{
ScrollViewer sv = (ScrollViewer)sender;
if (sv.HorizontalScrollBarVisibility == ScrollBarVisibility.Disabled)
{
sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
}
else
{
sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
}
return sender;
}
}
This works great.
source to share