THE WHAT
It’s bugged me for months, but I finally figured out a generic way to transition between views in WPF–a way that allows me to use whatever animation I want to for the transitions. And no, the VisualStateManager doesn’t help at all here–it’s only good for transitions within the same view.
There are frameworks and techniques out there already, but frankly, they all confused the hell out of me or seemed clunky. Another guy’s solution was OK, but I didn’t really like it for some reason that didn’t quite keep me up at night. Maybe, while simpler than most, it still seemed confusing, so I got the gist of it and made my own. It did, however, inspire my solution.
So I imported my helper library, and below is the only code you would need to get transitions going between views. You can use any animation you want, not just my fade in/out animation. If someone lets me know a good place to upload my solution, I’ll be happy to share.
THE CODE
There is a window with a red box (RedGridViewModel) and a blue box (BlueGridViewModel). Each box has a button below it that alternately changes the box to the color that it currently isn’t. When the button is pressed, the current box fades out, and the new box fades in. The button is disabled while the transition occurs, and both buttons can be pressed in rapid succession without crashing the program. I used two subordinate ViewModels here, but you can use however many you want.
For the sake of completeness, this is literally the only other code I wrote for this example (excluding the very small helper assembly that’s a little verbose for this thread):
I made use of the following:
WPF Data Pipes
Can’t find the original link, but binding wpf events to VM commands was important
Code Contracts Plugin, because I like code contracts
If there’s interest, I can post the plumbing code or upload the whole solution somewhere. It’s really not that long–two base classes, and a resource file with two styles and two storyboards.