views:

120

answers:

2

Hi all,

I have an usercontrol in silverlight that I'm trying to convert to custom control. The usercontrol is working. The customcontrol is working BUT has its storyboard not working.

the control is :

public class MyControl : Control
{
    public MyControl()
    {
        DefaultStyleKey = typeof(MyControl);
    }

    public static readonly DependencyProperty IsStartingProperty = DependencyProperty.Register("IsStarting", typeof(bool), typeof(MyControl), new PropertyMetadata(new PropertyChangedCallback(OnIsStartingChanged)));

    private static void OnIsStartingChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        MyControl control = obj as MyControl;
        if (control != null && control._layoutRoot != null && control._storyboard != null)
        {
            if ((bool)e.NewValue)
            {
                control._layoutRoot.Visibility = Visibility.Visible;
                control._storyboard.Begin();
            }
            else
            {
                control._layoutRoot.Visibility = Visibility.Collapsed;
                control._storyboard.Stop();
            }
        }
    }

    private Canvas _layoutRoot;
    private Storyboard _storyboard;

    public override void OnApplyTemplate()
    {
        _layoutRoot = GetTemplateChild("LayoutRoot") as Canvas;
        _storyboard = GetTemplateChild("IndicatorStoryboard") as Storyboard;
        base.OnApplyTemplate();
    }

    public bool IsStarting
    {
        get { return (bool)GetValue(IsStartingProperty); }
        set { SetValue(IsStartingProperty, value); }
    }
}

On debug, no error on control._storyboard.Begin();, but I can't see the animation ...

Does someone has an idea ? How to work with storyboard ?

Thanks in advance for any help

EDIT : Full source sample is available : http://vpclip.virtua-peanuts.net/WindowsPhoneApplication1.zip

A: 

This is off the top of my head, but what happens if you move the "base.OnApplyTemplate(); " as the first line in the OnApplyTemplate() function?

   public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        _layoutRoot = GetTemplateChild("LayoutRoot") as Canvas;
        _storyboard = GetTemplateChild("IndicatorStoryboard") as Storyboard;
    }

Does that help?

Todd Davis
thanks for your answer, but the same think.Note that I'm using WP7, maybe does this matter ...
Tim
I don't *think* WP7 should affect it. I'm curious, I see that you are turning the _layoutRoot Visible first... is the storyboard inside the layoutRoot? Could be a timing issue if so - try leaving it visible initially and see if the storyboard fires that way to test it. Also, any reason you can't add making the layoutRoot visible as part of the storyboard? (Not part of the question of course, but just wondering).
Todd Davis