VisualStateManager.GoToElementState(FrameworkElement, String, Boolean) Methode

Definition

Wechselt zwischen zwei Zuständen des Elements. Verwenden Sie diese Methode zum Wechseln von Zuständen, die von einer Anwendung und nicht von einem Steuerelement definiert werden.

public:
 static bool GoToElementState(System::Windows::FrameworkElement ^ stateGroupsRoot, System::String ^ stateName, bool useTransitions);
public static bool GoToElementState (System.Windows.FrameworkElement stateGroupsRoot, string stateName, bool useTransitions);
static member GoToElementState : System.Windows.FrameworkElement * string * bool -> bool
Public Shared Function GoToElementState (stateGroupsRoot As FrameworkElement, stateName As String, useTransitions As Boolean) As Boolean

Parameter

stateGroupsRoot
FrameworkElement

Das Stammelement, das den VisualStateManager enthält.

stateName
String

Der Zustand, in den der Übergang durchgeführt wird.

useTransitions
Boolean

true, wenn ein VisualTransition-Objekt für den Zustandsübergang verwendet werden soll, andernfalls false.

Gibt zurück

true, wenn der Zustand des Steuerelements erfolgreich gewechselt wurde, andernfalls false.

Ausnahmen

stateGroupsRoot ist null.

- oder -

stateName ist null.

Beispiele

Im folgenden Beispiel wird eine Anwendung erstellt, die den Benutzer auffordert, eine Zahl zwischen 1 und 100 zu erraten. Wenn der Benutzer eine Zahl eingibt, gibt die Anwendung an, ob die Schätzung zu hoch, zu niedrig oder richtig ist. Der folgende XAML-Code erstellt die Benutzeroberfläche für die Anwendung und definiert 4 Zustände: TooLow, TooHigh, Correctund Guessing. Wenn die Anwendung beginnt, befindet sie sich Guessing im Zustand. .

<Grid x:Name="LayoutRoot">
  <Grid.Background>
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
      <GradientStop Color="#FF022343" Offset="0"/>
      <GradientStop Color="#FF006BD2" Offset="1"/>
    </LinearGradientBrush>
  </Grid.Background>
  <VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="ResultStates">
      <VisualStateGroup.Transitions>
        <VisualTransition GeneratedDuration="0:0:0.5">
          <VisualTransition.GeneratedEasingFunction>
            <CubicEase EasingMode="EaseOut"/>
          </VisualTransition.GeneratedEasingFunction>
        </VisualTransition>
      </VisualStateGroup.Transitions>
      <VisualState x:Name="TooLow">
        <Storyboard>
          <ColorAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(Panel.Background).
              (GradientBrush.GradientStops)[1].(GradientStop.Color)" 
            Storyboard.TargetName="LayoutRoot">
            <EasingColorKeyFrame KeyTime="0" Value="#FFD22700"/>
          </ColorAnimationUsingKeyFrames>
          <ColorAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(Panel.Background).
              (GradientBrush.GradientStops)[0].(GradientStop.Color)" 
            Storyboard.TargetName="LayoutRoot">
            <EasingColorKeyFrame KeyTime="0" Value="#FFE2D7D0"/>
          </ColorAnimationUsingKeyFrames>
          <DoubleAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(UIElement.RenderTransform).
              (TransformGroup.Children)[0].(ScaleTransform.ScaleX)" 
            Storyboard.TargetName="viewbox">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
          </DoubleAnimationUsingKeyFrames>
          <DoubleAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(UIElement.RenderTransform).
              (TransformGroup.Children)[0].(ScaleTransform.ScaleY)" 
            Storyboard.TargetName="viewbox">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
          </DoubleAnimationUsingKeyFrames>
          <DoubleAnimationUsingKeyFrames
            Storyboard.TargetProperty="(Panel.Background).
              (GradientBrush.GradientStops)[0].(GradientStop.Offset)" 
            Storyboard.TargetName="LayoutRoot">
            <EasingDoubleKeyFrame KeyTime="0" Value="0.5"/>
          </DoubleAnimationUsingKeyFrames>
        </Storyboard>
      </VisualState>
      <VisualState x:Name="TooHigh">
        <Storyboard>
          <ColorAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(Panel.Background).
              (GradientBrush.GradientStops)[1].(GradientStop.Color)" 
            Storyboard.TargetName="LayoutRoot">
            <EasingColorKeyFrame KeyTime="0" Value="#FFF3EBE9"/>
          </ColorAnimationUsingKeyFrames>
          <ColorAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(Panel.Background).
              (GradientBrush.GradientStops)[0].(GradientStop.Color)" 
            Storyboard.TargetName="LayoutRoot">
            <EasingColorKeyFrame KeyTime="0" Value="#FFF12807"/>
          </ColorAnimationUsingKeyFrames>
          <DoubleAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(UIElement.RenderTransform).
              (TransformGroup.Children)[0].(ScaleTransform.ScaleX)" 
            Storyboard.TargetName="viewbox">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
          </DoubleAnimationUsingKeyFrames>
          <DoubleAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(UIElement.RenderTransform).
              (TransformGroup.Children)[0].(ScaleTransform.ScaleY)" 
            Storyboard.TargetName="viewbox">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
          </DoubleAnimationUsingKeyFrames>
          <DoubleAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(Panel.Background).
              (GradientBrush.GradientStops)[1].(GradientStop.Offset)" 
            Storyboard.TargetName="LayoutRoot">
            <EasingDoubleKeyFrame KeyTime="0" Value="0.5"/>
          </DoubleAnimationUsingKeyFrames>
        </Storyboard>
      </VisualState>
      <VisualState x:Name="Correct">
        <Storyboard>
          <ColorAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(Panel.Background).
              (GradientBrush.GradientStops)[0].(GradientStop.Color)" 
            Storyboard.TargetName="LayoutRoot">
            <EasingColorKeyFrame KeyTime="0" Value="#FF0E4302"/>
          </ColorAnimationUsingKeyFrames>
          <ColorAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(Panel.Background).
              (GradientBrush.GradientStops)[1].(GradientStop.Color)" 
            Storyboard.TargetName="LayoutRoot">
            <EasingColorKeyFrame KeyTime="0" Value="#FF14D200"/>
          </ColorAnimationUsingKeyFrames>
          <DoubleAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(UIElement.RenderTransform).
              (TransformGroup.Children)[0].(ScaleTransform.ScaleX)" 
            Storyboard.TargetName="viewbox">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
          </DoubleAnimationUsingKeyFrames>
          <DoubleAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(UIElement.RenderTransform).
              (TransformGroup.Children)[0].(ScaleTransform.ScaleY)" 
            Storyboard.TargetName="viewbox">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
          </DoubleAnimationUsingKeyFrames>
        </Storyboard>
      </VisualState>
      <VisualState x:Name="Guessing"/>
    </VisualStateGroup>
  </VisualStateManager.VisualStateGroups>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition/>
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition/>
    <ColumnDefinition Width="247"/>
  </Grid.ColumnDefinitions>
  <TextBlock Grid.Row="0" Grid.ColumnSpan="2" Margin="20,5,20,5" 
             FontSize="24" Foreground="White"
             Text="Guess a number between 1 and 100"/>
  <TextBox x:Name="Guess" Grid.Row="1" Margin="20,20,10,10" 
           TextWrapping="Wrap" FontSize="48" 
           TextChanged="OnTypingGuess"/>
  <Button x:Name="GuessButton" Grid.Row="1" Content="Guess" 
          Margin="10,20,20,10" Grid.Column="1" 
          FontSize="48" Click="OnGuess" 
          IsDefault="True"/>
  <Viewbox x:Name="viewbox" Grid.ColumnSpan="2" Grid.Row="2"
           Margin="20,10,20,20" RenderTransformOrigin="0.5,0.5">
    <Viewbox.RenderTransform>
      <TransformGroup>
        <ScaleTransform ScaleX="0" ScaleY="0"/>
        <SkewTransform/>
        <RotateTransform/>
        <TranslateTransform/>
      </TransformGroup>
    </Viewbox.RenderTransform>
    <TextBlock x:Name="Result" TextWrapping="Wrap"/>
  </Viewbox>
</Grid>

Der folgende Code zeigt die Logik der Anwendung. Wenn der Benutzer eine Zahl eingibt und auf die Schaltfläche klickt, ruft GoToElementState die Anwendung auf, zu TooHighwechseln TooLowoder Correct um dem Benutzer Feedback zu der Vermutung zu geben. Um dieses Beispiel auszuprobieren, rufen Sie die GenerateNumber -Methode auf, wenn die Anwendung beginnt.

int number;

private void GenerateNumber()
{
    System.Random r = new System.Random();
    number = r.Next(100) + 1;
}

private void OnGuess(object sender, System.Windows.RoutedEventArgs e)
{
    int guess;

    if (int.TryParse(this.Guess.Text, out guess))
    {
        if (guess < this.number)
        {
            VisualStateManager.GoToElementState(this.LayoutRoot, "TooLow", true);
            this.Result.Text = "Too Low!";
        }
        else if (guess > this.number)
        {
            VisualStateManager.GoToElementState(this.LayoutRoot, "TooHigh", true);
            this.Result.Text = "Too High!";
        }
        else
        {
            VisualStateManager.GoToElementState(this.LayoutRoot, "Correct", true);
            this.Result.Text = "Correct!";
        }
    }
}

private void OnTypingGuess(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
    VisualStateManager.GoToElementState(this.LayoutRoot, "Guessing", true);
    this.Result.Text = "";
}
Private number As Integer

Private Sub GenerateNumber()
    Dim r As New System.Random()
    number = r.Next(100) + 1
End Sub

Private Sub OnGuess(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim guess As Integer

    If Integer.TryParse(Me.Guess.Text, guess) Then
        If guess < Me.number Then
            VisualStateManager.GoToElementState(Me.LayoutRoot, "TooLow", True)
            Me.Result.Text = "Too Low!"
        ElseIf guess > Me.number Then
            VisualStateManager.GoToElementState(Me.LayoutRoot, "TooHigh", True)
            Me.Result.Text = "Too High!"
        Else
            VisualStateManager.GoToElementState(Me.LayoutRoot, "Correct", True)
            Me.Result.Text = "Correct!"
        End If
    End If
End Sub

Private Sub OnTypingGuess(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
    VisualStateManager.GoToElementState(Me.LayoutRoot, "Guessing", True)
    Me.Result.Text = ""
End Sub

Hinweise

Rufen Sie die GoToElementState -Methode auf, um Zustände für ein Element außerhalb eines ControlTemplate zu ändern (z. B. wenn Sie einen in oder VisualStateManagerDataTemplateWindowverwenden). Rufen Sie die GoToState -Methode auf, wenn Sie die Zustände in einem Steuerelement ändern, das in VisualStateManagerControlTemplateverwendet.

Gilt für: