Comment Feed for Channel 9 - Custom Per-Page Transitions for Windows Phone 7http://video.ch9.ms/ecn/previewImages/100/553419_100x75.jpgChannel 9 - Custom Per-Page Transitions for Windows Phone 7
Continuing our previous explorations of the TransitioningContentControl (here and
here – all WinPhone content
here), we will now take a look at how we can extend the TCC to allow transitions to be defined on a per-page basis. Simply put, we would like to have a default transition for all pages, and to provide a list of custom transitions to use on a page-by-page
basis. In addition to this article, you can
watch a screencast of making these changes, and source is linked at the end of the article.
Our first step is creating a simple class that will be used to hold Page-to-Transition mappings. That class is shown below:
namespace System.Windows.Controls
{
public class PageTransitionMapping
{
public string Page { get; set;
}
public string Transition { get; set;
}
}
}
I added this class to the TCC folder from the previous screencast (download here). Notice that I changed the namespace to match the TCC’s namespace.
Next, the TCC is modified, allowing us to provide Page-To-Transition mappings via the App.xaml file. See below:
public class TransitioningContentControl : ContentControl{ private List&lt;PageTransitionMapping&gt; _mappings = new List&lt;PageTransitionMapping&gt;(); public List&lt;PageTransitionMapping&gt; PageTransitionMappings { get { return _mappings; } } …
The _mappings field stores a List&lt;&gt; of PageTransitionMapping objects. These mapping objects are created by accessing the PageTransitionMappings property via App.xaml as shown below
&lt;phoneNavigation:PhoneApplicationFrame x:Name=&quot;RootFrame&quot; Source=&quot;/MainPage.xaml&quot;&gt; &lt;phoneNavigation:PhoneApplicationFrame.Template&gt; &lt;ControlTemplate&gt; &lt;layout:TransitioningContentControl Content=&quot;{TemplateBinding Content}&quot; Style=&quot;{StaticResource TransitioningStyle}&quot;&gt; &lt;layout:TransitioningContentControl.PageTransitionMappings&gt; &lt;layout:PageTransitionMapping Page=&quot;AprilCTP.Views.Favorites.Default&quot; Transition=&quot;SwingTransition&quot; /&gt; &lt;/layout:TransitioningContentControl.PageTransitionMappings&gt; &lt;/layout:TransitioningContentControl&gt; &lt;/ControlTemplate&gt; &lt;/phoneNavigation:PhoneApplicationFrame.Template&gt;
In this example, the Default.xaml page in the Views/Favorites folder has been mapped to use the SwingTransition. Just like before, all of the transitions used by the TCCC must be in the TCC’s Style definition (specifically, in the ControlTemplate’s VisualStateManager).
The next to last step is to update the way the TCC applies transitions to pages as they are displayed. To do this, we update the TCC’s StartTransition() method as shown below:
private void StartTransition(object oldContent, object newContent) { // both presenters must be available, otherwise a transition is useless. if (CurrentContentPresentationSite != null &amp;&amp; PreviousContentPresentationSite != null) { PageTransitionMapping transitionMapping = null; var newPage = newContent as PhoneApplicationPage; if (newPage != null) { string pageType = newPage.GetType().ToString(); transitionMapping = (from m in this.PageTransitionMappings where m.Page == pageType select m).SingleOrDefault(); } // and start a new transition if (!IsTransitioning || RestartTransitionOnContentChange) { IsTransitioning = true; if (transitionMapping != null) { Transition = transitionMapping.Transition; } else Transition = _standardTransition; CurrentContentPresentationSite.Content = newContent; PreviousContentPresentationSite.Content = oldContent; VisualStateManager.GoToState(this, NormalState, false); VisualStateManager.GoToState(this, Transition, true); } } }
There are a couple of important points. First, the CurrentContextPresentionSite.Content = and the PreviousContentPresentationState.Content = lines of code have been moved a bit from where they appeared in the original code. Second, there is no real error
handling for cases where you enter the wrong name for your custom transition inside the App.xaml mapping. This is more “proof of concept” code than production ready code. The TCC has a GetStoryboard method that you can use to make sure the transition is available
– it is just a matter of where you want to detect that and how you want to handle it in the case that a transition does not exist (falling back to the default transition is probably easiest).
The last thing we have to do is set the value for _standardTransition. We will set its value in the OnApplyTemplate() method inside the TCC. This method gets called one time when the TCC is first created and the template is applied. We will add the declaration
for _standardTransition immediately before the method (stick it where ever you like):
private string _standardTransition; /// &lt;summary&gt; /// Builds the visual tree for the TransitioningContentControl control /// when a new template is applied. /// &lt;/summary&gt; public override void OnApplyTemplate()
The OnApplyTemplate discovers the value of the TCC Transition property that was set in the Style in the App.xaml file. Setting _standardTransition to this value lets us use it whenever there is not a custom page transition defined. Update the OnApplyTemplate()
method as shown:
^^^^ REST OF METHOD ^^^^ _standardTransition = Transition; VisualStateManager.GoToState(this, NormalState, false); }
I have included the source for a
working solution here.
enWed, 04 Mar 2015 00:24:10 GMTWed, 04 Mar 2015 00:24:10 GMTRev9Re: Custom Per-Page Transitions for Windows Phone 7
Great Article,

Any chance for this to be updated to be compatible with the RTM version? As alot of stuff has changed since the April CTP release.