VisualStateManager in Silverlight: Initializing

I often find it necessary to include visual states such as “away” and “real” that are used to animate a control that is far away or visible, depending on some other condition.

Then the off state is usually the one that should be the initial state. As far as I understand, there is no way to determine the initial state in SL, but "base", which is actually not a state at all, but means that the appearance with the state manager is not active yet (none of them work, change the appearance of the control )

Of course, you can create a “base” to look “off”, but this means that the default view in Expression Blend is invisible (you also cannot “take out” the state on yourself).

To change the initial state, I tried

  • setting state in ctor of a control that does nothing and
  • setting the state in a sent call from ctor or the Loaded event, which shows an incorrect state for a second.

Thus, the problem is that no matter what the visual state manager does, it does not do it right away, but a noticeable split second is required to change the appearance.

(Setting the property directly for bootstrapping is, of course, another option, but it works only for UserControls: in the template control, I will need to enter another depprop parameter to bind the template to the template of the template, in which I believe that overkill starts .)

I suppose that covered it all, and I just need to live with an invisible base state?

I am using SL4.

+5
source share
1 answer

UserControl WPF Expression Blend (: , . ). UserControl , . , " " , "" . , , UserControl . : Base, "Hidden" "Visible" ( ).

. GoToStateAction ( UserControl), Loaded. UserControl "" :

enter image description here

<i:Interaction.Triggers>
  <i:EventTrigger>
    <ei:GoToStateAction TargetObject="{Binding ElementName=userControl}" StateName="Hidden"/>
  </i:EventTrigger>
</i:Interaction.Triggers>

-, . , , . " " , , 4 . ( Blend) "" 0 ( GoToStateAction "" , ). "" "", (, 4 ). . , "" " " "" , "" "" .

enter image description here


VisualState Custom Control

( UserControl) VisualStateManager , ( VisualState Loaded), , . , ( ) OnApplyTemplate(), Loaded. , VisualState Loaded , . OnApplyTemplate():

public class MyCustomControl : ContentControl
{
    // ... other code ....


    public MyCustomControl()
    {
        // avoid designer errors
        if (DesignerProperties.GetIsInDesignMode(this))
            return;

        Loaded += new RoutedEventHandlerMyCustomControl_Loaded);
    }

    // This probably won't be called until AFTER OnApplyTemplate() gets
    //  called, so don't expect for your control to even have a visual tree
    //  yet when your control is first being contructed at runtime.
    private void MyCustomControl_Loaded(object sender, RoutedEventArgs e)
    {

    }

    public override void OnApplyTemplate()
    {
        // Avoid Visual Studio 2010 designer exceptions
        // (Visual Studio can't handle the VisualState change at design-time)
        if (DesignerProperties.GetIsInDesignMode(this))
            return;

        base.OnApplyTemplate();

        // Now we know that the template has been applied, we have a visual tree,
        //  so state changes will work
        VisualStateManager.GoToState(this, "MyInitialState", false);
    }
}
+6

All Articles