Windows 运行时应用中的行为

本文关键字:运行时 应用 Windows | 更新日期: 2023-09-27 18:35:51

我的 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:时,它会自己拉起它;我不知道这对问题是否重要。

任何帮助将不胜感激。

Windows 运行时应用中的行为

在被提示退房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;
    }
}

这非常有效。