VisualStateManager.GoToState returns false and Visual State is not changed

倖福魔咒の 提交于 2020-01-16 04:35:08

问题


I have this XAML code:

       <Button x:Name="btnStartRecord" Visibility="Collapsed">
            <Button.BorderBrush>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FFF3883E" Offset="1"/>
                </LinearGradientBrush>
            </Button.BorderBrush>
        </Button>
        <Button x:Name="btnStopRecord" Visibility="Collapsed">
            <Button.BorderBrush>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FFF3883E" Offset="1"/>
                </LinearGradientBrush>
            </Button.BorderBrush>
        </Button>

<VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="MainStates">
                <VisualState x:Name="RecordIconState">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="btnStartRecord" Storyboard.TargetProperty="(UIElement.Visibility)">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Visible"/>
                            <!--<DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>-->
                        </ObjectAnimationUsingKeyFrames>
                        <!--<ObjectAnimationUsingKeyFrames Storyboard.TargetName="btnStopRecord" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>-->
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="StopRecordIconState">
                    <Storyboard>
                        <!--<ObjectAnimationUsingKeyFrames Storyboard.TargetName="btnStartRecord" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>-->
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="btnStopRecord" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                </VisualStateGroup>                
        </VisualStateManager.VisualStateGroups>

And in Code Behind, I call following function in OnLoad/Constructor :

private void SwitchRecordButtonContent()
    {
        {
            if (m_bRecording)
            {
                //btnStartStopRecord.Content = "StopRecord";
                VisualStateManager.GoToState(this, StopRecordIconState.Name, false);
                VisualState currentState = MainStates.CurrentState;
                Visibility temp = btnStartRecord.Visibility;
            }
            else
            {
                //btnStartStopRecord.Content = "StartRecord";
                bool op = VisualStateManager.GoToState((Button)this.btnStartRecord, RecordIconState.Name, false); // I get this always false
                VisualState currentState = MainStates.CurrentState;
                Visibility temp = btnStartRecord.Visibility;
            }
        }
    }

But I see no Visual state change and I cannot figure out why :(

Is there anything I am doing wrong?


回答1:


You are calling

VisualStateManager.GoToState((Button)this.btnStartRecord, RecordIconState.Name, false); // I get this always false

but based on a short glance you should be calling

VisualStateManager.GoToState(this, RecordIconState.Name, false);



回答2:


as atomaras mentioned in above comment, moved the code to the very root of the usercontrol and it worked :)



来源:https://stackoverflow.com/questions/20483686/visualstatemanager-gotostate-returns-false-and-visual-state-is-not-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!