如何在两个(或多个)XAML 文件之间共享 VisualStateManager?

发布于 2024-10-06 10:03:59 字数 1101 浏览 1 评论 0原文

我们正在编写一个基于 Prism 的 Silverlight 应用程序,并且我们在单独的模块中拥有一大堆页面。

页面之间的转换是通过导航事件处理的,每个模块都实现了以下方法,以在导航到页面时显示页面并在导航时隐藏页面:

public void Show()
{
    VisualStateManager.GoToState(this, "ShowState", true);
}

public void Hide()
{
    VisualStateManager.GoToState(this, "HideState", true);
}

目前“ShowState”和“HideState”在每个模块的 XAML 文件中定义,因此重复太多次了。

<Grid x:Name="LayoutRoot">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStates">
            <VisualState x:Name="ShowState">
                ...
            </VisualState>
            <VisualState x:Name="HideState">
                ...
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

其中 ... 表示每个转换的 Storyboard

我刚刚在 Storyboard 定义中发现了一个错误,目前我必须在所有文件中复制修复程序。如果每个文件中只有一个可以引用的 Storyboard 定义,那就更好了。

我整个早上都在寻找正确的语法,但一直没有运气。

如何在所有 XAML 文件之间共享此 VisualStateManager

We're writing a Prism based Silverlight application and we've got a whole bunch of pages in separate modules.

The transition between the pages is handled via navigation events and each module has the following methods implemented to show the page when navigated to and hide it when navigated from:

public void Show()
{
    VisualStateManager.GoToState(this, "ShowState", true);
}

public void Hide()
{
    VisualStateManager.GoToState(this, "HideState", true);
}

At the moment "ShowState" and "HideState" are defined in each module's XAML file so are duplicated far too many times.

<Grid x:Name="LayoutRoot">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStates">
            <VisualState x:Name="ShowState">
                ...
            </VisualState>
            <VisualState x:Name="HideState">
                ...
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

Where ... represents the Storyboard for each transition.

I've just spotted an error in the Storyboard definitions and at the moment I'm going to have to replicate the fix across all the files. It would be better if there was only one definition of the Storyboard which could be referenced in each file.

I've searched all morning for the right syntax but have had no luck what so ever.

How can I share this VisualStateManager between all our XAML files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淤浪 2024-10-13 10:03:59
<Storyboard x:Key="ShowStoryboard">
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="glow" Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

<VisualState x:Name="ShowState">
    <BeginStoryboard Storyboard="{StaticResource ShowStoryboard}"/>
</VisualState>

如上所示,可以在 XAML 中引用 Storyboard。最上面的部分是作为资源存储在某处的故事板。之后,您应该能够在 VisualState 中使用 BeginStoryboard 引用。

编辑:上述内容在 WPF 中似乎是可能的,但在 SL 中是不可能的。截至目前,SL 中似乎无法重用 StoryboardVisualState 。您仍然应该能够通过将 VisualStateManager 行为封装在应用于自定义控件的样式中来实现您想要执行的操作。这将为您提供您正在寻找的单点故障。

<Storyboard x:Key="ShowStoryboard">
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="glow" Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

<VisualState x:Name="ShowState">
    <BeginStoryboard Storyboard="{StaticResource ShowStoryboard}"/>
</VisualState>

Referencing your Storyboard within XAML can be done as seen above. With the top most portion being a Storyboard stored as a Resource somewhere. After that you should be able to use the BeginStoryboard reference within your VisualState.

EDIT: The above appears possible within WPF however it is not possible in SL. As of current it does not appear the abilty to reuse a Storyboard or VisualState is possible in SL. You should still be able to achieve what you are trying to do by encapsulating the VisualStateManager behavior within a style applied to a custom control. This would provide you the single point of failure you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文