views:

266

answers:

2

I'm working on a Silverlight 3.0 app, and about 1 out of every 10 or 20 times, a UserControl I've created won't load with the rest of the application. The rest of the application loads and functions correctly. So far, I've been exclusively launching this from visual studio, but I'd like to be sure that this isn't something that will happen when it's actually deployed.

Does anyone know what might be causing this? Has anyone else experienced this?

EDIT: the control's constructor (all animations are initialized)

        InitializeComponent();

        m_CenterXAnimation.Duration = new TimeSpan(0, 0, 0, 0, 800);
        m_CenterYAnimation.Duration = new TimeSpan(0, 0, 0, 0, 800);

        m_FadeOutAnimation.Duration = new TimeSpan(0, 0, 5);
        m_FadeInAnimation.Duration = new TimeSpan(0, 0, 5);
        m_FadeOutAnimation.To = 0;
        m_FadeInAnimation.To = 1;

        m_ScaleDownAnimation.Duration = new TimeSpan(0, 0, 0, 0, 800);
        m_ScaleUpAnimation.Duration = new TimeSpan(0, 0, 0, 0, 800);
        m_ScaleDownAnimation.To = 1;
        m_ScaleUpAnimation.To = 1.1;   

        App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
A: 

It sounds to me like you getting an exception within your UserControl that is not causing a full crash but is preventing your control from completing initialization. You could try debugging the initialization of the UserControl and it's bindings - start by placing a breakpoint in the unhandled exception handler of your App class.

Jeff Yates
That sounds reasonable, but the constructor is fairly simple (and getting added to the question as soon as I add this comment) and it doesn't seem like anything in it could be throwing an exception that would keep it from initializing, especially not only a fraction of the time.
oltman
Initialization doesn't just mean the constructor, it also means your XAML and all the bindings.
Jeff Yates
I was just coming back to add a comment addressing that :) The XAML is pretty vanilla; I've added no bindings, and the only non-standard control included is a simple path with a storyboard to animate it. Could any of these be causing the problem, do you think?
oltman
I have seen race conditions with storyboards before but I'm not that this is the problem in this instance. Have you tried disabling the storyboard to see if the problem goes away?
Jeff Yates
I haven't tried this. A particularly annoying part of the problem is that it happens very infrequently, so I have trouble reproducing it even without any changes. Where/how would the race condition be occurring?
oltman
Well, if something was relying on something else to exist but the order in which they were initialised could vary, for example.
Jeff Yates
A: 

I doubt the problem is with the UserControl. Sounds a like a timing issue. I've had similar problems but it was caused by other parts of the application - usually in the order events fire. Nothing wrong with your example code - the issue lies elsewhere.

RaoulRubin
Which events were causing this? Since I've deployed the app, I've noticed that it happens much more often (more frequent loading from a remote location than a local one.)
oltman
It was complicated and I've forgotten the details - sorry. I was loading the user controls dynamically in code (not xaml) after a request to a web service. It dynamically created an instance of the user control, then called some initialization and added child controls. This was happening before the UserControl Loaded event fired. In come cases this caused the control to not render. I moved some of the code to after Loaded event and it seemed to fix the problem.
RaoulRubin