行为在样式设置器中无法正常工作 [英] Behaviors are not working properly inside Style Setter

查看:37
本文介绍了行为在样式设置器中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows运行时应用程序中,我有一个主题,其样式具有为DoubleTapped动作定义的行为:

In my Windows-Runtime app, I have a theme with a style that has a Behavior defined for the DoubleTapped action:

这些是XML命名空间:

These are the XML Namespaces:

xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"

这是样式:

<Style x:Name="DisplayImage" TargetType="ScrollViewer">
    <Setter Property="VerticalScrollBarVisibility" Value="Hidden" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="ZoomMode" Value="Disabled" />
    <Setter Property="i:Interaction.Behaviors">
        <Setter.Value>
            <i:BehaviorCollection>
                <core:EventTriggerBehavior EventName="DoubleTapped">
                    <local:ScrollViewerDoubleTap />
                </core:EventTriggerBehavior>
            </i:BehaviorCollection>
        </Setter.Value>
    </Setter>
</Style>

这是我的行为:

[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;
    }
}

这就是我的使用方式:

<ScrollViewer Style="{StaticResource Image}" MaxWidth="1067">
    <Border BorderBrush="Black" BorderThickness="1">
        <Image Source="MyImage.png"/>
    </Border>
</ScrollViewer>

当我双击具有这种样式的页面上的第一张图像时,它可以完美工作;但是,当我双击页面上的其他图像时,行为代码将永远不会运行.我知道它永远不会运行,因为我使用断点来运行它,并且当我双击第一个图像而不是第二个图像时,它会中断.我将不胜感激为什么发生这种情况的任何提示.

When I double-tap the first image on a page that has this style, it works perfectly; however, when I double-tap the other images on the page, the behavior code is never run. I know it is never run because I ran it with breakpoints, and it would break when I double-tapped the first image, but not the second. I will appreciate any tips on why this is happening.

推荐答案

这行不通,因为行为,动作或触发器被设计为附加到单个元素.当您在样式的设置器中定义它时,就像您正在尝试将其与多个元素相关联,并且正如您已经看到的那样,仅当您与具有该样式的第一个元素进行交互时,才会调用触发器.

This won't work because behaviors, actions or triggers are designed to be attached to a single element. When you define it inside a style's setter, it's like you are trying to associate it with multiple elements and as you have already seen, the trigger is only called when you interact with the first element with this style.

有一种简单的方法可以解决此问题.基本上,您需要确保与此样式关联的每个元素都具有您创建的触发器的新实例.您可以将所有这些逻辑包装在附加属性中,然后您的样式只需引用该属性.

There's a simple way to fix this. Basically, you need to make sure each element that's associated with this style has a new instance of the trigger you have created. You can have all this logic wrapped inside an attached property and then your style will only need to reference this property.

<Style x:Name="DisplayImage" TargetType="ScrollViewer">
    <Setter Property="VerticalScrollBarVisibility" Value="Hidden" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="ZoomMode" Value="Disabled" />
    <Setter Property="local:FrameworkElementEx.AttachBehaviors" Value="True" />
</Style>

这是实现此附加属性的方式.

This is how this attached property is implemented.

public static class FrameworkElementEx
{
    public static bool GetAttachBehaviors(DependencyObject obj)
    {
        return (bool)obj.GetValue(AttachBehaviorsProperty);
    }

    public static void SetAttachBehaviors(DependencyObject obj, bool value)
    {
        obj.SetValue(AttachBehaviorsProperty, value);
    }

    public static readonly DependencyProperty AttachBehaviorsProperty =
        DependencyProperty.RegisterAttached("AttachBehaviors", typeof(bool), typeof(FrameworkElementEx), new PropertyMetadata(false, Callback));

    private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var behaviors = Interaction.GetBehaviors(d);

        var eventTriggerBehavior = new EventTriggerBehavior
        {
            EventName = "DoubleTapped"
        };
        eventTriggerBehavior.Actions.Add(new ScrollViewerDoubleTap());

        behaviors.Add(eventTriggerBehavior);
    }
}

这篇关于行为在样式设置器中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆