2016-07-31 59 views
0

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

你期望的結果,這可能僅是真的嗎?目前看來,TextBox將在0.2秒後可見,但它將完全透明。然後1秒鐘完全不透明,但2秒鐘後,它會再次完全透明。 – ChrisF

+0

是的,基本上文本應該出現和消失,但由於某種原因,動畫無法正常工作,也許它與控制模板或C#代碼有關,因爲動畫從我所瞭解的是正確的 – topoall

+0

如果你)向模板添加'x:Key =「Test」'並且b)在XAML中定義文本框。如果我在後面的代碼中定義文本框,我似乎無法使其工作。 – ChrisF

回答

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

然後在加載的處理程序,你可以去你想要的狀態。

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 App程序使用的參數。

作爲TextBox是一個類型的控制:

private void Instructions_Loaded(object sender, RoutedEventArgs e) 
    { 
     var result = VisualStateManager.GoToState(sender as Control, "Blue", true); 
    } 

應該工作。

而且該控件模板需求的關鍵,而不是一個名字*:

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

資源字典使用密鑰值,因爲它的關鍵,所以此刻的你沒有發現的資源都這麼instructions.Template是空值。

* 桌面應用

+0

錯誤:無法從'Windows.UI.Xaml.FrameworkElement'轉換爲'Windows.UI.Xaml.Controls.Control',將其更改爲var result = VisualStateManager.GoToState(發件人爲ContentControl,「Blue」,true);出現NullReference異常 – topoall

+0

@topoall - 你在哪裏得到這個錯誤?我有這個代碼在VS2015中工作。 – ChrisF

+0

我有VS 2015社區,[error](http://i.stack.imgur.com/icDjb.png)[error2](http://i.stack.imgur.com/WCVGN.png) – topoall

相關問題