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

时间:2010-12-06 14:22:31

标签: silverlight resources visualstatemanager

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

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

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

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

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

<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

1 个答案:

答案 0 :(得分:2)

<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行为封装在应用于自定义控件的样式中,您仍应该能够实现您要执行的操作。这将为您提供您正在寻找的单点故障。

相关问题