New Image Appears before Transition Effect

Sep 21, 2009 at 12:15 PM

I have an application that sets the content of a specific grid to loaded xaml from an external source per request from a controller. The control sends a message which tells my application the location of the new xaml. The application clears the original content and reloads from the new source. Because the messages are recieved on a thread other then the UI thread I have to use the dispatcher. I am attempting to have a transition effect occur as the content is changed, it is working  but on occasion the new content appears briefly before the transition effect fires. I belive it is possibly down to dispatcher priority but I'm not sure. A sample of my code is below, any advice is appreciated. pparams is an array of strings sent by the controller. effects is an array of transition effects. da is a double animation defined as DoubleAnimation da = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(2.0)), FillBehavior.HoldEnd);

Regards Nobby

//Store Old Image.

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send,
 new Action(() => { vb.Visual = _panel.Children[0]; }));

 this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send,
new Action(() => { vb.Viewbox = new Rect(0, 0, _panel.ActualWidth, _panel.ActualHeight); }));

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send,
new Action(() => { vb.ViewboxUnits = System.Windows.Media.BrushMappingMode.Absolute; }));

//Clear existing panel.
WPFThreadingExtensions.BeginInvokeIfRequired(_panel,
() => { _panel.Children.Clear(); }, DispatcherPriority.Normal);

//Write new panel.
WPFThreadingExtensions.BeginInvokeIfRequired(_panel,
 () => { _panel.Children.Add((UIElement)ClsIO.LoadXAML(pparams[0])); },
DispatcherPriority.Normal);

//Transition Effect

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send,
new Action(() => { effects[Convert.ToInt32(pparams[2])].OldImage = vb; }));

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send,
new Action(() => { effects[Convert.ToInt32(pparams[2])].BeginAnimation(TransitionEffect.ProgressProperty, da); }));

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send,
new Action(() => { _panel.Effect = effects[Convert.ToInt32(pparams[2])]; }));