如何在两个(或多个)XAML 文件之间共享 VisualStateManager?
我们正在编写一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如上所示,可以在 XAML 中引用 Storyboard。最上面的部分是作为资源存储在某处的故事板。之后,您应该能够在 VisualState 中使用 BeginStoryboard 引用。
编辑:上述内容在 WPF 中似乎是可能的,但在 SL 中是不可能的。截至目前,SL 中似乎无法重用
Storyboard
或VisualState
。您仍然应该能够通过将 VisualStateManager 行为封装在应用于自定义控件的样式中来实现您想要执行的操作。这将为您提供您正在寻找的单点故障。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
orVisualState
is possible in SL. You should still be able to achieve what you are trying to do by encapsulating theVisualStateManager
behavior within a style applied to a custom control. This would provide you the single point of failure you are looking for.