문제

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

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

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top