4

속성을 변경하기위한 내 코드가 작동하지 않아 문제가 무엇인지 전혀 알 수 없지만 어쩌면 그렇게 할 수 있습니다.VisualStateManager를 올바르게 사용하려면 어떻게해야합니까?

다음은 오류를 재현하는 내 코드의 간단한 예입니다. 이 내 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); 
    } 
} 

은 이론적으로,이 버튼 색을 "아쿠아"로 변경해야하지만, 아무 반응이 없습니다 .

답변

5

내용을 ContentControl 안에 넣고 페이지 대신 컨트롤에 VisualState를 적용합니다. 대신 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); 
관련 문제