Expression Blend WPF应用程序无法在定义的状态之间更改

时间:2015-05-13 09:11:36

标签: c# wpf xaml expression-blend

在我在Expression Blend中完成的WPF应用程序中,我定义了一些状态(如'SunAnimation','SnowAnimation'等)。现在,如果我在程序未运行时在这些状态之间切换(在设计模式下),一切正常。但是当我运行该程序并尝试使用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>

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:0)

再次看一下你的代码,因为无法理解状态变化为什么不起作用。 认为这会有所帮助。 首先将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