Spaghetti Code: Evolutionary Programming with Jason Bock

6/29/2010 12:30:00 PM

Spaghetti Code talks with Jason Bock about how Evolutionary Programming lets you use genetic principles to find solutions to complex problems. You can get the source code, presentation deck, and more @ http://www.jasonbock.net/JB/Code/EvolvingNET.zip

  • Direct Download - click here
  • Subscribe - click here
  • iTunes - click here
  • Tags:

    SpaghettiCode

    Custom Per-Page Transitions in Windows Phone 7

    6/1/2010 3:57:49 PM

    Continuing our previous explorations of the TransitioningContentControl (hereand 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.  Put simply, we would like to be able to have a default transition for all pages, and then provide a list of custom transitions that will be used on a page-by-page basis.  You can also watch a screencast  making these changes.  Source is linked at the end of the article.

    Our first step is to create a simple class that will be used to hold Page-to-Transition mappings.  That class is shown below:

    1. namespace System.Windows.Controls
    2. {
    3.      public class PageTransitionMapping
    4.      {
    5.           public string Page { get; set; }
    6.           public string Transition { get; set; }
    7.      }
    8. }

    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 to allow us to provide Page-To-Transition mappings via the App.xaml file.  See below:

    1. public class TransitioningContentControl : ContentControl
    2. {
    3.  
    4. private List<PageTransitionMapping> _mappings = new List<PageTransitionMapping>();
    5. public List<PageTransitionMapping> PageTransitionMappings { get { return _mappings; } }
    6.   …

    The _mappings field stores a List<> of PageTransitionMapping objects.  These mapping objects are created by accessing the PageTransitionMappings property via App.xaml as shown below

    1. <phoneNavigation:PhoneApplicationFrame x:Name="RootFrame" Source="/MainPage.xaml">
    2. <phoneNavigation:PhoneApplicationFrame.Template>
    3. <ControlTemplate>
    4. <layout:TransitioningContentControl Content="{TemplateBinding Content}" Style="{StaticResource TransitioningStyle}">
    5. <layout:TransitioningContentControl.PageTransitionMappings>
    6. <layout:PageTransitionMapping Page="AprilCTP.Views.Favorites.Default" Transition="SwingTransition" />
    7. </layout:TransitioningContentControl.PageTransitionMappings>
    8. </layout:TransitioningContentControl>
    9. </ControlTemplate>
    10. </phoneNavigation:PhoneApplicationFrame.Template>

    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 – the ControlTemplate’s VisualStateManager specifically).

    The next 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:

    1. private void StartTransition(object oldContent, object newContent)
    2. {
    3. // both presenters must be available, otherwise a transition is useless.
    4. if (CurrentContentPresentationSite != null && PreviousContentPresentationSite != null)
    5. {
    6. PageTransitionMapping transitionMapping = null;
    7. var newPage = newContent as PhoneApplicationPage;
    8.  
    9. if (newPage != null)
    10. {
    11. string pageType = newPage.GetType().ToString();
    12. transitionMapping = (from m in this.PageTransitionMappings where m.Page == pageType select m).SingleOrDefault();
    13. }
    14.  
    15.  
    16.  
    17. // and start a new transition
    18. if (!IsTransitioning || RestartTransitionOnContentChange)
    19. {
    20. IsTransitioning = true;
    21.  
    22. if (transitionMapping != null)
    23. {
    24. Transition = transitionMapping.Transition;
    25. }
    26. else
    27. Transition = _standardTransition;
    28.  
    29. CurrentContentPresentationSite.Content = newContent;
    30. PreviousContentPresentationSite.Content = oldContent;
    31. VisualStateManager.GoToState(this, NormalState, false);
    32. VisualStateManager.GoToState(this, Transition, true);
    33. }
    34. }
    35. }

    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.  Secondly, 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 then how you want to handle it in case a transition does not exist (fall back to the default transition is probably the 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):

    1. private string _standardTransition;
    2. /// <summary>
    3. /// Builds the visual tree for the TransitioningContentControl control
    4. /// when a new template is applied.
    5. /// </summary>
    6. 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 let’s us use it whenever there is not a custom page transition defined.  Update the OnApplyTemplate() method as shown:

    1. ^^^^ REST OF METHOD ^^^^
    2. _standardTransition = Transition;
    3. VisualStateManager.GoToState(this, NormalState, false);
    4. }

     

    I have included the source for a working solution here.

    Tags:

    Spaghetti Code: XNA Game Development with Chris Williams

    6/1/2010 9:17:22 AM

    Spaghetti Code Talks XNA Game Development for Windows, XBOX and Windows Phone 7 with XNA MVP Chris Williams.

  • Direct Download - click here
  • Subscribe - click here
  • iTunes - click here
  • Tags:

    SpaghettiCode

    Spaghetti Code: Becoming an Entrepreneur with Scott Davis and Jeff Pesek

    6/1/2010 9:01:39 AM

    Spaghetti Code talks with Scott Davis and Jeff Pesek about becoming your own business person.  We cover everything for the types of businesses you can establish, what you can “sell”, how to get started, and much more.

  • Direct Download - click here
  • Subscribe - click here
  • iTunes - click here

  • Tags:

    SpaghettiCode

    Powered by BlogEngine.NET 1.6.0.0
    Theme by Mads Kristensen

    About the author

    Jeff Brand Jeff Brand

    This is the personal web site of Jeff Brand, self-proclaimed .NET Sex Symbol and All-Around Good guy. Content from my presentations, blog, and links to other useful .NET information can all be found here.

    E-mail me Send mail


    Calendar

    <<  February 2012  >>
    MoTuWeThFrSaSu
    303112345
    6789101112
    13141516171819
    20212223242526
    2728291234
    567891011

    View posts in large calendar

    My Twitter Updates

    XBOX
    Live

    Recent comments

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2012

    Sign in