Skip to main content

Microsoft Silverlight

Answered Question VisualStateManager.GoToState not changing states sometimesRSS Feed

(0)

pbrooks
pbrooks

Contributor

Contributor

2671 points

355 Posts

Silverlight MVP

VisualStateManager.GoToState not changing states sometimes

 I am working on a custom control that uses VisualStateManager.GoToState to change to its various states (MouseOver, Normal).  It works fine for a few state changes but then it inevitably seems to silently fail for some state changes.  The strange thing is that the behavior is only occuring when I have multiple instances of the same control on my form.  I haven't been able to pin down why this is happening, but VisualStateManager.GoToState is returning true even when it fails to change state.  Also, the control goes back to the correct state when I resize the browser window.  Anybody have any ideas about this?  Thanks!

If this has answered your question, please click on "Mark as Answer" on this post.

Thanks,
Page Brooks
Silverlight MVP, MCSD
PageBrooks.com | @pbrooks

Dave Relyea
Dave Relyea

Participant

Participant

1084 points

249 Posts

Microsoft

Re: VisualStateManager.GoToState not changing states sometimes

If you are animating the color of a SolidColorBrush, we have some issues with that, since a feature to share brushes isn't playing nicely with VSM.

pbrooks
pbrooks

Contributor

Contributor

2671 points

355 Posts

Silverlight MVP

Re: VisualStateManager.GoToState not changing states sometimes

 Wow, I spent at least 4 hours trying to figure out what I was doing wrong and I think you nailed it right on the head (at least I hope).  I changed my states to temporarily use Opacity and the states do seem to function correctly.  In other words, I can move my mouse as fast as I want and for as long as I want and the controls seem to transition to the correct states. I am using ColorAnimation for my states and I am animating

(Shape.Fill).(GradientBrush.GradientStops)[X].(GradientStop.Color)
(Shape.Stroke).(GradientBrush.GradientStops)[X].(GradientStop.Color)

Does the issue you describe apply to this as well?  If so, is there something I can do to work around it?  Thanks a bunch for your help on this, I have been pulling my hair out! Big Smile

If this has answered your question, please click on "Mark as Answer" on this post.

Thanks,
Page Brooks
Silverlight MVP, MCSD
PageBrooks.com | @pbrooks

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Re: Re: VisualStateManager.GoToState not changing states sometimes

Hello, I think to work around the issue Dave described, you need to create multiple elements for different groups. For example, with a CheckBox, if you use a single Rectangle for both Mouse Over and Checked state, and animate its Fill, you'll run into problems because the two states are in different groups. Instead, you can create two Rectangles with proper ZIndex. You animate the Opacity or Visibility property of those Rectangles. For your Custom Control, you should be careful with groups, so states in one group won't interfere with each other.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

pbrooks
pbrooks

Contributor

Contributor

2671 points

355 Posts

Silverlight MVP

Re: Re: VisualStateManager.GoToState not changing states sometimes

My custom control only has one VSM group, so I was thinking that there was no way my states could interfere with each other.  My control is a star rating control, similar to what you would see at Netflix.  If you hover your mouse over the stars, all the preceding stars (including the mouse over star) move to the highlighted state.  The states are ColorAnimations that change from a dull grey to a bright yellow.  The issue I am having is when I move my mouse over the last star it hangs on the state.  The funny thing is that it doesn't do it right away, it only does it after moving the mouse around for a while.  That is why I thought it might be a resource sharing problem.  Since changing the opacity seems to work without hanging up on the states, I guess I could do what you said and create 2 stars, normal and highlighted, and toy with the opacity.  I just feel like it would be more elegant if I could use Color animations instead.  I would like to know more about the SolidColorBrush/VSM issue that Dave mentioned so I can be sure to avoid it in the future.  Thanks again for any help!

If this has answered your question, please click on "Mark as Answer" on this post.

Thanks,
Page Brooks
Silverlight MVP, MCSD
PageBrooks.com | @pbrooks

pbrooks
pbrooks

Contributor

Contributor

2671 points

355 Posts

Silverlight MVP

Re: Re: VisualStateManager.GoToState not changing states sometimes

 I built a sample control that exhibits the same behavior as my other control so you can get an idea for what I am trying to explain. If you move your mouse back and forth across the lights, you will see they turn on in an incremental fashion.  If you move back and forth a good bit, eventually the last light will hang in the "on" state.  If you play around with it enough, other random lights will hang in the "on" state.  See the screenshots below:

 

While re-building this sample, I learned that the problem only occurs (as far as I can tell) if my Visual State contains a ColorAnimation that targets a GradientBrush.  I used the exact same code with a simple color animation (SolidColorBrush) and it works as expected.  I've included the source code and a live demo.  Thanks for any help you can offer on this!

Live Demo:
http://pagebrooks.com/demos/ColorAnimationVSM/SilvelightLightBulbMatrixTestPage.html

Source Code:
http://pagebrooks.com/files/source/silverlight/coloranimationvsm.zip

If this has answered your question, please click on "Mark as Answer" on this post.

Thanks,
Page Brooks
Silverlight MVP, MCSD
PageBrooks.com | @pbrooks

Dave Relyea
Dave Relyea

Participant

Participant

1084 points

249 Posts

Microsoft
Answered Question

Re: Re: VisualStateManager.GoToState not changing states sometimes

I think it is the same issue. This works fine for me (for as long as I tried it). You could also use a Grid as your root and put another ellipse with the "off" color in front of the "on" ellipse, and animate the opacity or visibility of that.

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:LightBulb">
                    <Ellipse x:Name="LightBulbEllipse" Height="30" Width="30" Opacity=".5">
                        <vsm:VisualStateManager.VisualStateGroups>
                            <vsm:VisualStateGroup x:Name="RatingModeStates">
                                <vsm:VisualState x:Name="Normal"  />
                                <vsm:VisualState x:Name="LightOn">
                                    <Storyboard>
                                        <DoubleAnimation
                                            Storyboard.TargetName="LightBulbEllipse"
                                            Storyboard.TargetProperty="Opacity"
                                            BeginTime="00:00:00" Duration="0:0:0"
                                            To="1"/>
                                    </Storyboard>
                                </vsm:VisualState>
                            </vsm:VisualStateGroup>
                        </vsm:VisualStateManager.VisualStateGroups>
                        <Ellipse.Fill>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FFF1FF00"/>
                                <GradientStop Color="#FFCEA61B" Offset="1"/>
                            </LinearGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                </ControlTemplate>
            </Setter.Value>

        </Setter>

pbrooks
pbrooks

Contributor

Contributor

2671 points

355 Posts

Silverlight MVP

Re: Re: VisualStateManager.GoToState not changing states sometimes

Is this something that will be fixed before release?  I was able to accomplish my goal with the said opacity workaround.  Unfortunately, that's just not as elegant as I would like because in my real application, I am animating the colors of a complicated Path and I just don't really like having all that duplicated XAML (on/off states) in there.  Either way, I'm going to mark your reply as 'answered' since it does fix the problem, but it would be nice to know if this is in fact a bug or simply a limitation of Silverlight.  Thanks a bunch!

If this has answered your question, please click on "Mark as Answer" on this post.

Thanks,
Page Brooks
Silverlight MVP, MCSD
PageBrooks.com | @pbrooks

Dave Relyea
Dave Relyea

Participant

Participant

1084 points

249 Posts

Microsoft

Re: Re: VisualStateManager.GoToState not changing states sometimes

Oh, we're working on it.

aquamoon
aquamoon

Member

Member

2 points

2 Posts

Re: Re: VisualStateManager.GoToState not changing states sometimes

another workaround move the storyboard to your usercontrol.resources, give it a name and start it in code with

(Resources["MyName"] as Storyboard).Begin();

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities