如何设置文本框的可视状态?

时间:2016-07-31 11:14:35

标签: c# windows visualstatemanager

1.我无法设置文本框的可视状态,我的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#c​​ode:

        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);

1 个答案:

答案 0 :(得分:0)

您正在尝试更改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,它的GoToStateFrameworkElement作为第一个参数 - 这是传统桌面应用程序使用的参数。还有一个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还需要一个Key,而不是Name *:

<ControlTemplate x:Key="instructions_text2" TargetType="TextBox">

资源字典使用Key值作为键,因此目前您根本找不到资源,因此instructions.Template为空。

* 这可能仅适用于桌面应用程序