For smooth animations, you can see the MS samples : Animation
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi!
I have difficulties with smooth animations. Therefore I decided to try out having the main window on it's own thread.
I have not found any example/practices for how to handle bindings.
Basically everything the window needs is in the datacontext, but of course then what i am getting is:
The calling thread cannot access this object because a different thread owns it.
Is there a source where I could read about a proper way to implement this?
Does anyone have experience with this approach in regard to animations?
Thanks!
I use this code for the dedicated thread:
Thread thread = new Thread(() =>
{
TopWindow = new Window();
TopWindow.DataContext = TopModel;
TopWindow.Show();
TopWindow.Closed += (sender1, e1) => TopWindow.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
For smooth animations, you can see the MS samples : Animation
Sorry in my case it's wpf storyboards and control animations..
Create your TopModel
on that thread like this:
Thread thread = new Thread(() =>
{
TopWindow = new Window();
TopWindow.DataContext = new TopModel();
TopWindow.Show();
TopWindow.Closed += (sender1, e1) => TopWindow.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
you might need the TopWindow
object to Close
the window later so you can have a reference of that like this:
TopWindow TopWindow = null;
Thread thread = new Thread(() =>
{
TopWindow = new Window();
TopWindow.DataContext = new TopModel();
TopWindow.Show();
TopWindow.Closed += (sender1, e1) => TopWindow.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
and later you can close the window like this:
TopWindow.Dispatcher.Invoke(TopWindow.Close);
You also don't need a ViewModel, TopModel, for such window that is overlaid on MainWindow
for some splash animation. You create the animation in the constructor or somewhere in the window and hook that animation into the Loaded/Activated
event of that window.
Did you read the sources ?
Several ones use StoryBoards (BeginStoryboard and so on)