0

Я создал настраиваемый элемент управления, который наследует от contentControl. Элемент управления использует анимацию PlaneProjection.RotationX, изменяясь от открытого до закрытого и наоборот.VisualStateManager.GoToState useTransitions Игнорируется в пользовательском контроле UWP

Я хочу, чтобы начальное состояние управления было в закрытом состоянии. Когда приложение запускается, переход от открытого к закрытому показан, хотя я установил changeVisualState(false).

Что я делаю неправильно?

Мой код:

public sealed class FlipPanel : ContentControl 
{ 
    private VisualState collapsedState; 
    private FrameworkElement contentElement; 

    public FlipPanel() 
    { 
     this.DefaultStyleKey = typeof(FlipPanel); 
    } 

    public bool IsOpen 
    { 
     get { return (bool)GetValue(IsOpenProperty); } 
     set { SetValue(IsOpenProperty, value); } 
    } 

    public static readonly DependencyProperty IsOpenProperty = 
     DependencyProperty.Register("IsOpen", typeof(bool), typeof(FlipPanel), new PropertyMetadata(false, onIsOpenChanged)); 

    private static void onIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 

     FlipPanel flipPanel = d as FlipPanel; 
     flipPanel.changeVisualState(true); 
    } 

    private void changeVisualState(bool useTransitions) 
    { 
     Debug.WriteLine(IsOpen.ToString()); 
     if (IsOpen) 
     { 
      if (contentElement != null) 
      { 
       contentElement.Visibility = Visibility.Visible; 
      } 
      VisualStateManager.GoToState(this, "Opening", useTransitions); 
     } 
     else 
     { 
      VisualStateManager.GoToState(this, "Closing", useTransitions); 
     } 
    } 

    protected override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     contentElement = (FrameworkElement)GetTemplateChild("Content"); 

     if (contentElement != null) 
     { 
      collapsedState = (VisualState)GetTemplateChild("Closing"); 

      if ((collapsedState != null) && (collapsedState.Storyboard != null)) 
      { 
       collapsedState.Storyboard.Completed += (object sender, object e) => 
       { 
        contentElement.Visibility = Visibility.Collapsed; 
       }; 
      } 

      changeVisualState(false); 
     }   
    } 
} 

и мой Style

<Style TargetType="local:FlipPanel" > 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:FlipPanel"> 
       <Grid> 

        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="ViewStates"> 
          <VisualState x:Name="Opening"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)" Storyboard.TargetName="Content"> 
             <EasingDoubleKeyFrame KeyTime="0" Value="-90"/> 
             <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Closing"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)" Storyboard.TargetName="Content"> 
             <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
             <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="90"/> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 

        <Border BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         Background="{TemplateBinding Background}"> 
         <Grid> 
          <ContentPresenter Content="{TemplateBinding Content}" x:Name="Content"> 
           <ContentPresenter.Projection> 
            <PlaneProjection CenterOfRotationX="0.5"/> 
           </ContentPresenter.Projection> 
          </ContentPresenter> 
         </Grid> 
        </Border> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

ответ

0

Я думаю, что это происходит потому, что вы делаете Content Collapsed в Completed случае раскадровки, а затем установить changeVisualState(false);, он будет делать ничего, поскольку «закрывающая» раскадровка завершена.

Я изменил свой код OnApplyTemplate, как это и это работает:

protected override void OnApplyTemplate() 
{ 
    base.OnApplyTemplate(); 
    contentElement = (FrameworkElement)GetTemplateChild("Content"); 
    if (contentElement != null) 
    { 
     if (!IsOpen) 
      contentElement.Visibility = Visibility.Collapsed; 
     else 
      changeVisualState(true); 
    } 
} 

enter image description here

+0

Спасибо. Только один вопрос. Таким образом, установка 'useTransitions' в' false' в методе 'VisualStateManager.GoToState' будет по-прежнему запускать анимацию? – PutraKg

+0

@PutraKg, я не имел в виду, что когда вы устанавливаете useTransitions в false в 'OnApplyTemplate()', анимация завершена, поэтому она не работает ... –

Смежные вопросы