我的更改属性的代码无法正常工作,我完全不知道出了什么问题,但也许你知道。
这是一个简化的示例,可以重现错误。 这是我的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”,但什么都没有发生。