0
votes

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?

1

1 Answers

1
votes

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.