我的Windows应用商店应用中有一组ScrollViewer
,需要在双击时始终执行操作(即,扩展为完整大小)。它们位于不同的文件中,或者我可能只是将它放在该文件的代码隐藏中:
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;
}
}
我被告知要调查行为,所以我研究了行为,并在几个在线教程的帮助下,我能够想出这个:
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;
}
}
}
现在,所有内容都编译得很好,但我无法将其附加到XAML:
XML命名空间:
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:local="using:MyApp.Folder"
xmlns:global="using:MyApp"
相关代码:
<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>
当我尝试转到此页面时,没有任何反应,当我调试时,它会给我这个错误消息:
WinRT information: Cannot add instance of type 'LearnOneNote.ViewboxDoubleTap' to a collection of type 'Microsoft.Xaml.Interactivity.ActionCollection'. [Line: 25 Position: 30]
此外,它告诉我ViewboxDoubleTap
不在namespace MyApp
,但是当我输入<global:
时,它会将其拉出来;我不知道这对这个问题是否重要。
任何帮助将不胜感激。
答案 0 :(得分:2)
在被要求查看IAction
(谢谢你,franssu)之后,我想出了这个:
[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;
}
}
这很有效。