问题
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