【问题标题】:How can I share a VisualStateManager between two (or more) XAML files?如何在两个(或更多)XAML 文件之间共享 VisualStateManager?
【发布时间】:2010-12-07 23:26:41
【问题描述】:

我们正在编写一个基于 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

【问题讨论】:

  • 您似乎想将“行为”封装在自定义控件中。这样,您只需根据需要重用控件,它将封装您的 Storyboard,从而提供一个参考点来解决出现的问题。我的猜测是这类似于扩展器之类的东西;为该容器内的任何孩子提供通用的功能。

标签: silverlight resources visualstatemanager


【解决方案1】:
<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 行为封装在应用于自定义控件的样式中,您仍然应该能够实现您想要做的事情。这将为您提供您正在寻找的单点故障。

【讨论】:

  • 我希望引用整个 VisualStateManager,但是将 Storyboard 定义放在一个地方就可以了 ;)
  • 这不起作用。 BeginStoryboard 不被识别为有效的 XAML,如果我只是这样做 &lt;VisualState ... Storyboard={...}/&gt; 我会收到初始化错误。
  • @ChrisF BeginStoryboard 在 SL 中确实是活跃的;但是它在触发器中的作用有限,并且可能在不可接受的 VisualState 中(它在 WPF 中),让我看看我还能鼓起什么...
  • @ChrisF 使用 SL 看起来不可能......但是,不创建自定义控件然后定义包含所需 VSM 工作的样式然后重用自定义控件的原因是什么?跨度>
  • 我开始认为自定义控件是要走的路。不过,将 VSM/Storyboard 作为资源会更容易一些;)。如果你想更新你的答案,我会接受。
猜你喜欢
  • 2010-09-12
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多