我希望最终确定应用程序的外观和感觉,但在将过渡动画应用于一个非常基本的问题时遇到困难。
在附件中您可以找到我正在努力解决的代码。我已经注释掉了我在开发和原型(prototype)设计过程中使用的 setter 。它们的工作方式正如我所期望的那样。
Storyboard过渡无法以任何方式或形式进行。只添加一个 Storyboard过渡与所有或任何 setter 相结合“有效”,因为我没有收到任何运行时错误,但过渡根本没有出现。
我想知道我是否没有完全误解 Storyboard的工作原理。
我遇到的错误是:
Multiple animations in the same containing Storyboard cannot target the same property on a single element.
这是简化的代码:
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:1"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="StateA">
<!--<VisualState.Setters>
<Setter Target="FirstElement.Visibility" Value="Visible" />
<Setter Target="SecondElement.Visibility" Value="Collapsed" />
</VisualState.Setters>-->
<Storyboard>
<PopOutThemeAnimation TargetName="SecondElement"/>
<PopInThemeAnimation TargetName="FirstElement"/>
</Storyboard>
</VisualState>
<VisualState x:Name="StateB">
<!-- <VisualState.Setters>
<Setter Target="FirstElement.Visibility" Value="Collapsed" />
<Setter Target="SecondElement.Visibility" Value="Visible" />
</VisualState.Setters>-->
<Storyboard>
<PopOutThemeAnimation TargetName="FirstElement"/>
<PopInThemeAnimation TargetName="SecondElement"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle Name="FirstElement"
Fill="Blue"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="200"
Height="200"
DoubleTapped="GoToStateB" />
<Rectangle Name="SecondElement"
Fill="Red"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="200"
Height="200"
DoubleTapped="GoToStateA" />
</Grid>
后面有以下简单代码:
private void GoToStateA(object sender, DoubleTappedRoutedEventArgs e)
{
// do something
VisualStateManager.GoToState(this, "StateA", true);
}
private void GoToStateB(object sender, DoubleTappedRoutedEventArgs e)
{
// do something else
VisualStateManager.GoToState(this, "StateB", true);
}
编辑:JustinXL指出的“解决方案”是需要删除以下Xaml元素,运行时错误就会消失:
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:1"/>
</VisualStateGroup.Transitions>
我不太清楚为什么会出现这种情况(真正的错误或只是一个晦涩的设计问题)。
最佳答案
实际上,更简单的解决方案是完全删除 VisualStateGroup
中的 Transitions
。
另请注意,我添加了 IsHitTestVisible
更改,以便 FirstElement
能够接收点击。
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="StateA">
<VisualState.Setters>
<Setter Target="SecondElement.(UIElement.IsHitTestVisible)"
Value="False" />
</VisualState.Setters>
<Storyboard>
<PopOutThemeAnimation TargetName="SecondElement" />
<PopInThemeAnimation TargetName="FirstElement" />
</Storyboard>
</VisualState>
<VisualState x:Name="StateB">
<Storyboard>
<PopOutThemeAnimation TargetName="FirstElement" />
<PopInThemeAnimation TargetName="SecondElement" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
关于c# - 尝试使用 Xaml Storyboard同时淡入一个元素并淡出另一个元素时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402852/