Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have a WPF .NET 4.5 application following the MVVM pattern (MVVM_Light framework).

I have a User-control (Parent) that contains a content control; this content control is bound to a user-control(child) property in the underlying ViewModel. A button/command on the parent usercontrol changes the Content Control's bound usercontrol.

enter image description here

My problem is that the child viewmodel has a heavy database procedure in its constructor and thus the UI hangs while that usercontrol is loading. I want to display a loading animation while the child viewmodel is created and then loaded.

What I tried: I have tried adding blend animations to the loaded event of the usercontrol, but that did not work.

Also

I created a usercontrol that is a loading animation and placed it as the default content of the content control. But the animation stops when the button is clicked and the new usercontrol is loading.

QUESTION: How do I have a loading animation while another usercontrol is loading?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Do your data access stuff in a Background Thread.

Then put some IsBusy property in the ViewModel and show some "Loading Message" when that becomes true:

public void DoHeavyStuff()
{
   IsBusy = true;
   Task.Factory.StartNew(() => GetDataFromDB())
               .ContinueWith(x => IsBusy = false);
}

Remove all heavy operations from constructors. That's a bad design.

share|improve this answer
    
thanks, this is the direction I am trying to go. I need the records from the constructor operation right away, so I am not sure where else in the view model I would put the data access as my viewmodel classes do not have a "loaded event" equivilant. Do you have any suggestions. – J King Jun 10 '13 at 21:33
    
@JKing put all heavy code in Tasks, call those tasks from the constructors if needed, but do not put the heavy code in constructors themselves. Then use continuations to do stuff when tasks complete. If using .Net 4.5, you may also leverage async/await. – HighCore Jun 10 '13 at 22:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.