Silverlight:在代码隐藏中切换 CustomVisualStateManager 的 VisualStates
我一直在尝试解决以下问题: 在 Expression Blend 3 中为不同视觉状态创建自定义动画(更改网格上多个元素的大小/不透明度)时,它会在网格本身而不是控件样式中创建视觉状态组,并将其定义为 CustomVisualStateManager。
<Grid x:Name="LayoutRoot" Background="White" Height="500" HorizontalAlignment="Left" VerticalAlignment="Top" Width="500">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="MyVisualStateGroup">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.3000000">
<VisualTransition.GeneratedEasingFunction>
<CircleEase EasingMode="EaseIn"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="State1"/>
<VisualState x:Name="State2">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="myBox" Storyboard.TargetProperty="(FrameworkElement.Height)">
<EasingDoubleKeyFrame KeyTime="00:00:00" Value="360"/>
<!-- omitting other storyboard animations here for clarity -->
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<VisualStateManager.CustomVisualStateManager>
<ic:ExtendedVisualStateManager/>
</VisualStateManager.CustomVisualStateManager>
<!-- omitting other grid elements here for clarity -->
</Grid>
对我来说没问题,但问题是,当我尝试什么都没有发生时,我无法在代码隐藏中切换状态
VisualStateManager.GoToState(this, "State1", true);
,因为控件本身没有定义这些视觉状态,如“
VisualStateManager.GetVisualStateGroups(this);
如果我尝试
VisualStateManager.GetVisualStateGroups(LayoutRoot);
它显示了我所需要的” 所示。但我无法将 LayoutRoot 传递给 VisualStateManager,因为它需要 Control 类型的参数,而 Grid 则不需要。 我的问题是 - 我如何在代码隐藏中访问/更改此 CustomVisualStateManager 的状态?
i've been trying to deal with the following problem:
When creating a custom animations for different visual states in Expression Blend 3, which change size/opacity of multiple elements on the grid, it creates the visual state groups in the grid itself rather than in control style and defines it as CustomVisualStateManager.
<Grid x:Name="LayoutRoot" Background="White" Height="500" HorizontalAlignment="Left" VerticalAlignment="Top" Width="500">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="MyVisualStateGroup">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.3000000">
<VisualTransition.GeneratedEasingFunction>
<CircleEase EasingMode="EaseIn"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="State1"/>
<VisualState x:Name="State2">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="myBox" Storyboard.TargetProperty="(FrameworkElement.Height)">
<EasingDoubleKeyFrame KeyTime="00:00:00" Value="360"/>
<!-- omitting other storyboard animations here for clarity -->
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<VisualStateManager.CustomVisualStateManager>
<ic:ExtendedVisualStateManager/>
</VisualStateManager.CustomVisualStateManager>
<!-- omitting other grid elements here for clarity -->
</Grid>
It's ok with me, but problem is, i can't switch states, in code-behind when i try
VisualStateManager.GoToState(this, "State1", true);
nothing happens, because the control itself doesn't have these visualstates defined, as shown by
VisualStateManager.GetVisualStateGroups(this);
If i try
VisualStateManager.GetVisualStateGroups(LayoutRoot);
it shows exactly what i need. But i cannot pass LayoutRoot to VisualStateManager because it needs an argument of Control type, which Grid isn't.
My question is - how can i access/ change states of this CustomVisualStateManager in code-behind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您启用 FluidLayout 时,CustomVisualStateManager 就在那里。除非您的项目中涉及布局变形(即您尝试使用状态从一种布局平滑地动画到另一种布局),否则您可以将其关闭。自定义 VSM 的存在不应对 VSM 的使用产生任何影响。
视觉状态标记始终位于顶级容器内,因此这是完全正常的。顺便说一句,这可能只是您的示例中的一个拼写错误,但您显示的代码实际上尝试设置一个其中没有任何内容的状态,因此这可能不是所需的结果。
否则,在 UserControl 上调用 VisualStateManager.GoToState 应该可以工作。这是我刚刚制作的一个有效的示例:
这是一个简单的 Silverlight 示例应用程序,带有一个主页和一个我添加到主页的用户控件。主页非常简单:
有一个用户控件的实例和两个按钮。我们稍后将了解这些按钮的作用。首先让我们看一下 UserControl (UserControl1):
正如您所看到的,在一个视觉状态组中定义了两种视觉状态,它们只是在用户控件的布局根上设置了一种颜色。
主页上的两个按钮连接到事件处理程序,如下所示:
第一个按钮仅调用页面上 UserControl 上的 VisualStateManager.GoToState。第二个调用用户控件一侧的函数来执行相同的操作。我只是使用这两种方法来表明这两个选项都可用 - 您可以从 UC 的外部或内部调用 VSM.GoToState。用户控件的 SetState() 方法如下所示:
当您运行应用程序时,用户控件将首先显示其基本状态,即绿色。当您按下 State 1 按钮时,它会转到 State1,这会通过从外部调用 VSM.GoToState() 将 UC 设置为蓝色。当您按下状态 2 按钮时,通过从内部调用 VSM,它会切换为红色。
从您发布的片段中,除了我在开头提到的一个问题之外,我看不出出了什么问题。但是,我的小样本可能会帮助您了解您的情况有何不同。
希望有帮助...
The CustomVisualStateManager is just there when you enable FluidLayout. Unless you have layout morphing involved in your project (i.e. you are trying to use states to animate smoothly from one layout to another), you can switch it off. The presence of the custom VSM should not make any difference in the usage of VSM.
The visual state markup always is inside the top level container, so that is perfectly normal. BTW, this might be just a typo in your sample, but the code you show actually tries to set a state that has nothing in it, so that might not be the desired result.
Otherwise, calling VisualStateManager.GoToState on the UserControl should work. Here is an example I just made that works:
This is a simple Silverlight example app, with a main page and a user control that I added to the main page. The main page is really simple:
There is an instance of my user control, and two buttons. We'll look at what the buttons do in a second. First let's look at the UserControl (UserControl1):
As you can see, there are two visual states defined in one visual state group that just set a color on the layout root of the user control.
The two buttons on the main page are wired up to event handlers that look as follows:
The first one just calls VisualStateManager.GoToState on the UserControl on the page. The second one calls a function iside of the user control that does the same thing. I just used both methods to show that both options are available - you can call VSM.GoToState from the outside or the inside of a UC. The SetState() method of the user control looks as follows:
When you run the app, the user control will first show up in its base state , which is green. When you press the State 1 button it goes to State1, which sets the UC to blue by calling VSM.GoToState() from the outside. When you press the State 2 button, it switches to red, by calling VSM from the inside.
From the snippets you posted, I can't see what is going wrong, short of the one issue that I mentioned at the beginning. However, my little sample might help you to see what is different in your case.
Hope that helps...