如何在两个(或多个)XAML 文件之间共享 VisualStateManager? [英] How can I share a VisualStateManager between two (or more) XAML files?

查看:38
本文介绍了如何在两个(或多个)XAML 文件之间共享 VisualStateManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在编写一个基于 Prism 的 Silverlight 应用程序,我们在不同的模块中有一大堆页面.

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

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

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>

其中 ... 代表每个过渡的 Storyboard.

Where ... represents the Storyboard for each transition.

我刚刚在 Storyboard 定义中发现了一个错误,目前我将不得不在所有文件中复制修复.如果 Storyboard 只有一个定义可以在每个文件中引用,那就更好了.

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.

如何在我们所有的 XAML 文件之间共享这个 VisualStateManager?

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

推荐答案

<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 中引用您的故事板.最上面的部分是作为资源存储在某处的故事板.之后,您应该可以在 VisualState 中使用 BeginStoryboard 引用.

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.

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

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.

这篇关于如何在两个(或多个)XAML 文件之间共享 VisualStateManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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