VisualStateManager.GoToState returns false and Visual State is not changed

4k views Asked by At

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?

2

There are 2 answers

0
ashish nirkhe On BEST ANSWER

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

2
Filip Skakun On

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