Expression Blend WPF 应用程序无法在定义的状态之间更改
Expression Blend WPF Application Can't Change between Defined States
在我在 Expression Blend
中完成的 WPF 应用程序中,我定义了一些状态(如 'SunAnimation'、'SnowAnimation' 等)。现在,如果我在程序未 运行ning(在设计模式下)时在这些状态之间切换,则一切正常。但是当我 运行 程序并尝试使用 VisualStateManager.GoToState(...)
更改状态时,它根本不起作用。
我在后面的文件中的代码是
void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
VisualStateManager.GoToState(this, Dynamic_State.Text.ToString(), true);
}
Dynamic_State 是一个 TextBlock,其中根据在 ComboBox 中选择的内容自动加载状态名称(通过 DataBinding)。
在 WPF 文件中,我定义了几个故事板,如下所示:
<Storyboard x:Key="SunAnimation" RepeatBehavior="Forever">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="Rays">
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="36"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.OpacityMask).(GradientBrush.GradientStops)[2].(GradientStop.Offset)" Storyboard.TargetName="ellipse">
<EasingDoubleKeyFrame KeyTime="0" Value="0.95"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="0.221"/>
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="0.95"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
我还定义了一些 ControlStoryboardAction
东西(根据教程,但我不确定这是否有用或有必要......)以及 VisualStateGroups
如下所示:
<i:Interaction.Triggers>
<i:EventTrigger>
<ei:ControlStoryboardAction Storyboard="{StaticResource SunAnimation}"/>
<ei:ControlStoryboardAction Storyboard="{StaticResource RainAnimation}"/>
<ei:ControlStoryboardAction Storyboard="{StaticResource CloudAnimation}"/>
<ei:ControlStoryboardAction Storyboard="{StaticResource SnowAnimation}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<VisualStateManager.CustomVisualStateManager>
<ei:ExtendedVisualStateManager/>
</VisualStateManager.CustomVisualStateManager>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="WeatherStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="None"/>
<VisualState x:Name="Sunny">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Sun">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Rainy">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Rain">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Sun">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="WeatherGrid">
<EasingColorKeyFrame KeyTime="0" Value="#FF00427A"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="WeatherGrid">
<EasingColorKeyFrame KeyTime="0" Value="#FF637685"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Cloudy">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="clouds">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Snowy">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="snowflakes">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="path6">
<EasingColorKeyFrame KeyTime="0" Value="#FF9EA19F"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="path7">
<EasingColorKeyFrame KeyTime="0" Value="#FF9EA19F"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="path8">
<EasingColorKeyFrame KeyTime="0" Value="#FF9EA19F"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="path9">
<EasingColorKeyFrame KeyTime="0" Value="#FF9EA19F"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="path6">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="path7">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="path8">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="path9">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
知道我做错了什么吗?
再次查看您的代码,因为看不出为什么状态更改不起作用。
认为这会有所帮助。
首先将您的 VisualStateManager 和 VisualStates 移到 Grid
LayoutRoot
下,例如
<Grid x:Name="LayoutRoot" Background="Black" Width="960" Height="540" DataContext="{Binding Source={StaticResource SampleWeatherData}}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="WeatherStates">
//all your other VisualStates
</VisualStateManager.VisualStateGroups>...
然后将您的代码稍微更改为
void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
VisualStateManager.GoToElementState(LayoutRoot, "Sunny", true);
}
编辑 不确定您是否必须完成此答案的第一阶段。测试一下然后告诉我
我认为这应该可行。 GL
在我在 Expression Blend
中完成的 WPF 应用程序中,我定义了一些状态(如 'SunAnimation'、'SnowAnimation' 等)。现在,如果我在程序未 运行ning(在设计模式下)时在这些状态之间切换,则一切正常。但是当我 运行 程序并尝试使用 VisualStateManager.GoToState(...)
更改状态时,它根本不起作用。
我在后面的文件中的代码是
void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
VisualStateManager.GoToState(this, Dynamic_State.Text.ToString(), true);
}
Dynamic_State 是一个 TextBlock,其中根据在 ComboBox 中选择的内容自动加载状态名称(通过 DataBinding)。
在 WPF 文件中,我定义了几个故事板,如下所示:
<Storyboard x:Key="SunAnimation" RepeatBehavior="Forever">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="Rays">
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="36"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.OpacityMask).(GradientBrush.GradientStops)[2].(GradientStop.Offset)" Storyboard.TargetName="ellipse">
<EasingDoubleKeyFrame KeyTime="0" Value="0.95"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="0.221"/>
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="0.95"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
我还定义了一些 ControlStoryboardAction
东西(根据教程,但我不确定这是否有用或有必要......)以及 VisualStateGroups
如下所示:
<i:Interaction.Triggers>
<i:EventTrigger>
<ei:ControlStoryboardAction Storyboard="{StaticResource SunAnimation}"/>
<ei:ControlStoryboardAction Storyboard="{StaticResource RainAnimation}"/>
<ei:ControlStoryboardAction Storyboard="{StaticResource CloudAnimation}"/>
<ei:ControlStoryboardAction Storyboard="{StaticResource SnowAnimation}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<VisualStateManager.CustomVisualStateManager>
<ei:ExtendedVisualStateManager/>
</VisualStateManager.CustomVisualStateManager>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="WeatherStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="None"/>
<VisualState x:Name="Sunny">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Sun">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Rainy">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Rain">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Sun">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="WeatherGrid">
<EasingColorKeyFrame KeyTime="0" Value="#FF00427A"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="WeatherGrid">
<EasingColorKeyFrame KeyTime="0" Value="#FF637685"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Cloudy">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="clouds">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Snowy">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="snowflakes">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="path6">
<EasingColorKeyFrame KeyTime="0" Value="#FF9EA19F"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="path7">
<EasingColorKeyFrame KeyTime="0" Value="#FF9EA19F"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="path8">
<EasingColorKeyFrame KeyTime="0" Value="#FF9EA19F"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="path9">
<EasingColorKeyFrame KeyTime="0" Value="#FF9EA19F"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="path6">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="path7">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="path8">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="path9">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
知道我做错了什么吗?
再次查看您的代码,因为看不出为什么状态更改不起作用。
认为这会有所帮助。
首先将您的 VisualStateManager 和 VisualStates 移到 Grid
LayoutRoot
下,例如
<Grid x:Name="LayoutRoot" Background="Black" Width="960" Height="540" DataContext="{Binding Source={StaticResource SampleWeatherData}}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="WeatherStates">
//all your other VisualStates
</VisualStateManager.VisualStateGroups>...
然后将您的代码稍微更改为
void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
VisualStateManager.GoToElementState(LayoutRoot, "Sunny", true);
}
编辑 不确定您是否必须完成此答案的第一阶段。测试一下然后告诉我
我认为这应该可行。 GL