Execute command after view is loaded WPF MVVM
That's because even though technically the view is loaded (i.e: all the components are ready in memory), your app is not idle yet, and thus the UI isn't refreshed yet.
Setting a command using interaction triggers on the Loaded
event is already good, as there is no better event to attach to.
Now to really wait until the UI is shown, do this in your StartProgress()
(I'm assuming here that this is the name of the method that StartProgressCommand
point to):
public void StartProgress(){ new DispatcherTimer(//It will not wait after the application is idle. TimeSpan.Zero, //It will wait until the application is idle DispatcherPriority.ApplicationIdle, //It will call this when the app is idle dispatcherTimer_Tick, //On the UI thread Application.Current.Dispatcher); }private static void dispatcherTimer_Tick(object sender, EventArgs e){ //Now the UI is really shown, do your computations}
You could use the Dispatcher for this and set the priority to ApplicationIdle so that it will on execute when everything has finished
Application.Current.Dispatcher.Invoke( DispatcherPriority.ApplicationIdle, new Action(() => { StartProgressCommand.Invoke(args); }));
more information on the dispatcher http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcherpriority.aspx
cheers.ste.
another way to do it:
define this xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
and xmlns:mi="http://schemas.microsoft.com/expression/2010/interactions"
on your usercontrol XAML and add Microsoft.Expression.Interactions assembly on your project. use CallMethodAction on your trigger, just as bellow:
<i:Interaction.Triggers> <i:EventTrigger EventName="Loaded"> <mi:CallMethodAction TargetObject="{Binding}" MethodName="StartProgressCommand"/> </i:EventTrigger></i:Interaction.Triggers>
Put the triger inside the root element of your usercontrol, e.g: grid. And change your StartProgressCommand, in your ViewModel class, from command to plain old regular Method, e.g:
public void StartProgressCommand(){ /* put your program logic here*/}
It'll run the method exactly one time every time your user control rendered.