【发布时间】:2010-01-22 16:41:58
【问题描述】:
有什么方法可以将 VisualStateManager 与我的 ChildWindow 子类一起使用?对 VisualStateManager 的调用什么都不做,而我所做的谷歌搜索暗示实现这一点的唯一方法是手动调用 Storyboards。这太草率了,而且容易出错。有没有人找到实现它的方法?
更新了示例代码。要使用它,只需创建一个新的 Silverlight 项目,然后通过单击主页上的按钮调用 ExampleWindow.ShowWindow()。即使构造函数设置了应该隐藏按钮的状态,您也会看到按钮。
XAML(ExampleWindow.xaml):
<controls:ChildWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
x:Class="Client.Windows.ExampleWindow"
Title="Example">
<Grid x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ExampleStateGroup">
<VisualState x:Name="ExampleBaseState">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.Opacity)">
<EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<VisualStateManager.CustomVisualStateManager>
<ic:ExtendedVisualStateManager/>
</VisualStateManager.CustomVisualStateManager>
<Button x:Name="button" Content="You Shouldn't See Me" Grid.ColumnSpan="2" Width="150" Height="150" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</controls:ChildWindow>
代码隐藏(ExampleWindow.xaml.cs):
using System.Windows;
using System.Windows.Controls;
namespace Client.Windows
{
public partial class ExampleWindow : ChildWindow
{
public ExampleWindow()
{
InitializeComponent();
VisualStateManager.GoToState( this, "ExampleBaseState", true );
}
public static void ShowWindow()
{
var w = new ExampleWindow();
w.Show();
}
}
}
【问题讨论】:
-
我想不出为什么 ChildWindow 不能使用 VisualStateManager,也许你应该给我们看一些你的代码?
-
我认为这与具有 Open 和 Closed VisualStates 的 ChildWindow 的模板有关,因此 VisualTree 对 VisualStateGroups 的搜索停在那里并且不会继续到控件的状态(尽管它在设计时在 Blend 中工作正常-时间)。
标签: silverlight xaml animation visualstatemanager states