UWP VisualStateManager.GotoState无法正常工作

时间:2018-11-17 05:40:46

标签: c# uwp visualstatemanager

我有一个自定义控件MediaPlayerControl。在其模板中,我定义了VisualStateGroup

<VisualStateGroup x:Name="SizeState">
    <VisualState x:Name="Normal" />
    <VisualState x:Name="LargeSize" >
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MediaInfo_Border" Storyboard.TargetProperty="(Grid.ColumnSpan)" >
                 <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
             </ObjectAnimationUsingKeyFrames>
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MediaInfo_Border" Storyboard.TargetProperty="(Grid.RowSpan)" >
                 <DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
             </ObjectAnimationUsingKeyFrames>
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PrimaryMediaControl_Border" Storyboard.TargetProperty="HorizontalAlignment" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="Center"/>
             </ObjectAnimationUsingKeyFrames>
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MediaTransportControls_Timeline_Border" Storyboard.TargetProperty="(Grid.Column)" >
                 <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
             </ObjectAnimationUsingKeyFrames>
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MediaTransportControls_Timeline_Border" Storyboard.TargetProperty="(Grid.ColumnSpan)" >
                 <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
             </ObjectAnimationUsingKeyFrames>
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MoreButton" Storyboard.TargetProperty="Visibility" >
                 <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
             </ObjectAnimationUsingKeyFrames>
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SecondaryTrim_MenuFlyOut" Storyboard.TargetProperty="Visibility" >
                 <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
             </ObjectAnimationUsingKeyFrames>
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TrimButton" Storyboard.TargetProperty="Visibility" >
                 <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
             </ObjectAnimationUsingKeyFrames>
         </Storyboard>
    </VisualState>
</VisualStateGroup>

SizeChanged事件中,我调用ChangeState()函数:

private void MediaPlayerControl_SizeChanged(object sender, object e)
{
    ChangeState();
}
private void ChangeState()
{
    string state = "";
    if (Window.Current.Bounds.Width < 600)
    {
        state = "Normal";
    }
    else
    {
        state = "LargeSize";
    }
    VisualStateManager.GoToState(this, state, false);
}

尽管VisualStateManager.GoToState返回true,并且UI更改为LargeSize状态,但在短时间内它会自动更改为Normal状态。

如果我使用LayoutUpdated事件,它可以正常工作,但是性能不好。

我可以将GotoStateSizeChanged事件一起使用吗?

0 个答案:

没有答案