我更改属性的代码不起作用,我完全不知道出了什么问题,但也许你知道。
下面是我的代码的一个简化示例,它复制了错误。这是我的Xaml代码:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="MyButton"
Height="100"
Width="300"
Content="Click"
FontSize="40"
FontWeight="Bold"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Background="Red" Click="MyButton_Click"/>
</Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="Blue">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyButton"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="Aqua"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>以下是“守则”:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void MyButton_Click(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this, "Blue", true);
}
}在理论上,这应该改变按钮颜色时,点击"Aqua“,但什么都没有发生。
发布于 2014-04-10 14:55:05
将内容放在ContentControl中,然后将VisualState应用到控件上,而不是在Page上。
另外,使用ObjectAnimation,代替ColorAnimation来动画按钮的颜色。
<ContentControl x:Name="contentControl">
<ContentControl.Template>
<ControlTemplate>
<Grid
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="MyButton"
Height="100"
Width="300"
Content="Click"
FontSize="40"
FontWeight="Bold"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Background="Red" Click="Button_Click"/>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CustomGroups">
<VisualState x:Name="Blue">
<Storyboard>
<ColorAnimation Storyboard.TargetName="MyButton"
Storyboard.TargetProperty="Background.Color"
To="Aqua"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>在后面的代码中,传递内容控制而不是页面:
VisualStateManager.GoToState(contentControl, "Blue", true);https://stackoverflow.com/questions/22990695
复制相似问题