如何设置文本框的视觉状态?
How do I set the visual state of a textbox?
1.I 无法设置我的文本框的视觉状态,使用我的 C# 代码可以显示文本,但不会显示动画 运行,对我的 C# 代码的任何想法或更正都会有所帮助让我明白
下面是我的 xaml:
<ControlTemplate x:Name="instructions_text2" TargetType="TextBox">
<Grid>
<TextBlock TextWrapping="Wrap" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Press Start and drag!" Foreground="#FFCB1717" FontSize="30" FontFamily="AR DELANEY" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CustomGroups">
<VisualState x:Name="Blue">
<Storyboard x:Name="Storyboard1" RepeatBehavior="Forever">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" >
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:0.2">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" >
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
c#代码:
TextBox instructions = new TextBox();
instructions.Template = Resources["instructions_text2"] as ControlTemplate;
instructions.Width = playArea.ActualWidth;
instructions.Height = playArea.ActualHeight;
VisualStateManager.GoToState(instructions, "Blue", true);
playArea.Children.Add(instructions);
您正试图在 TextBox 准备就绪之前更改它的视觉状态。事实上,您正试图在将状态添加到可视化树之前更改状态。
将您的代码更改为:
TextBox instructions = new TextBox();
instructions.Template = Resources["instructions_text2"] as ControlTemplate;
instructions.Width = playArea.ActualWidth;
instructions.Height = playArea.ActualHeight;
instructions.Loaded += Instructions_Loaded;
playArea.Children.Add(instructions);
然后在 Loaded 处理程序中,您可以转到您想要的状态。
private void Instructions_Loaded(object sender, RoutedEventArgs e)
{
var result = VisualStateManager.GoToState(sender as FrameworkElement, "Blue", true);
}
注意:VisualStateManager 有两个版本。我使用的是在命名空间 System.Windows
中,它的 GoToState
将 FrameworkElement
作为第一个参数——这是传统桌面应用程序使用的参数。 Windows.UI.Xaml
中还有一个 VisualStateManager,它将 Control
作为第一个参数 - 这是新 Windows 应用程序使用的参数。
因为TextBox
是一种控制方式:
private void Instructions_Loaded(object sender, RoutedEventArgs e)
{
var result = VisualStateManager.GoToState(sender as Control, "Blue", true);
}
应该可以。
此外,ControlTemplate 需要一个键,而不是名称 *:
<ControlTemplate x:Key="instructions_text2" TargetType="TextBox">
资源字典使用 Key 值作为它的键,所以目前您根本找不到资源,所以 instructions.Template
为 null。
* 这可能仅适用于桌面应用程序
1.I 无法设置我的文本框的视觉状态,使用我的 C# 代码可以显示文本,但不会显示动画 运行,对我的 C# 代码的任何想法或更正都会有所帮助让我明白
下面是我的 xaml:
<ControlTemplate x:Name="instructions_text2" TargetType="TextBox">
<Grid>
<TextBlock TextWrapping="Wrap" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Press Start and drag!" Foreground="#FFCB1717" FontSize="30" FontFamily="AR DELANEY" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CustomGroups">
<VisualState x:Name="Blue">
<Storyboard x:Name="Storyboard1" RepeatBehavior="Forever">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" >
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:0.2">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" >
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
c#代码:
TextBox instructions = new TextBox();
instructions.Template = Resources["instructions_text2"] as ControlTemplate;
instructions.Width = playArea.ActualWidth;
instructions.Height = playArea.ActualHeight;
VisualStateManager.GoToState(instructions, "Blue", true);
playArea.Children.Add(instructions);
您正试图在 TextBox 准备就绪之前更改它的视觉状态。事实上,您正试图在将状态添加到可视化树之前更改状态。
将您的代码更改为:
TextBox instructions = new TextBox();
instructions.Template = Resources["instructions_text2"] as ControlTemplate;
instructions.Width = playArea.ActualWidth;
instructions.Height = playArea.ActualHeight;
instructions.Loaded += Instructions_Loaded;
playArea.Children.Add(instructions);
然后在 Loaded 处理程序中,您可以转到您想要的状态。
private void Instructions_Loaded(object sender, RoutedEventArgs e)
{
var result = VisualStateManager.GoToState(sender as FrameworkElement, "Blue", true);
}
注意:VisualStateManager 有两个版本。我使用的是在命名空间 System.Windows
中,它的 GoToState
将 FrameworkElement
作为第一个参数——这是传统桌面应用程序使用的参数。 Windows.UI.Xaml
中还有一个 VisualStateManager,它将 Control
作为第一个参数 - 这是新 Windows 应用程序使用的参数。
因为TextBox
是一种控制方式:
private void Instructions_Loaded(object sender, RoutedEventArgs e)
{
var result = VisualStateManager.GoToState(sender as Control, "Blue", true);
}
应该可以。
此外,ControlTemplate 需要一个键,而不是名称 *:
<ControlTemplate x:Key="instructions_text2" TargetType="TextBox">
资源字典使用 Key 值作为它的键,所以目前您根本找不到资源,所以 instructions.Template
为 null。
* 这可能仅适用于桌面应用程序