Click here to Skip to main content
15,891,951 members
Articles / Desktop Programming / XAML

Robust UserControl Navigation in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.80/5 (6 votes)
18 Feb 2011CPOL6 min read 25.9K   762   15  
A simple and very effective way to manage navigation between UserControls in a Silverlight project.
  • RCNavController.zip
    • RCNavController.sln
    • RCNavController.Web
    • RCNavController
      • App.xaml
      • App.xaml.cs
      • Bin
        • Debug
          • AppManifest.xaml
          • ar
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • bg
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • ca
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • cs
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • da
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • de
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • el
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • es
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • et
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • eu
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • fi
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • fr
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • he
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • hr
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • hu
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • id
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • it
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • ja
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • ko
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • lt
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • lv
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • ms
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • nl
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • no
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • pl
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • pt
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • pt-BR
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • RCNavController.dll
          • RCNavController.xap
          • RCNavControllerTestPage.html
          • ro
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • ru
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • sk
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • sl
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • sr-Cyrl-CS
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • sr-Latn-CS
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • sv
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • System.ComponentModel.DataAnnotations.dll
          • System.ComponentModel.DataAnnotations.xml
          • System.Windows.Controls.Data.Input.dll
          • System.Windows.Controls.Data.Input.xml
          • th
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • tr
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • uk
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • vi
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • zh-Hans
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
          • zh-Hant
            • System.ComponentModel.DataAnnotations.resources.dll
            • System.Windows.Controls.Data.Input.resources.dll
        • Release
      • Controls
      • Global.cs
      • Login.xaml
      • Login.xaml.cs
      • MainPage.xaml
      • MainPage.xaml.cs
      • NavController.xaml
      • NavController.xaml.cs
      • obj
        • Debug
          • Controls
          • TempPE
      • Properties
      • RCNavController.csproj
      • RCNavController.csproj.user
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace RCNavController
{
    public partial class NavController : UserControl
    {
        public NavController()
        {
            InitializeComponent();
        }

        private UIElement waitingControl = null;
        public void NavigateToControl(UIElement newControl)
        {
            waitingControl = newControl;
            WriteableBitmap bitmap = new WriteableBitmap(ContentContainer, null); //take screenshot
         
            ImageOverlay.Source = bitmap;//set the source of the image control
            bitmap.Invalidate();//kill the bitmap
            ImageOverlay.Visibility = System.Windows.Visibility.Visible; 
            GrayOverlay.Visibility = System.Windows.Visibility.Visible;
            Loader.Visibility = System.Windows.Visibility.Visible;
            DisplayOverlays.Begin();
        }

        public void Hide()
        {
            HideOverlays.Begin();//start hiding all the overlays to display the new control
        }

        private void DisplayOverlays_Completed(object sender, EventArgs e)
        {
            ContentContainer.Child = waitingControl; // change the control to the new one
            waitingControl = null;
        }

        private void HideOverlays_Completed(object sender, EventArgs e)
        {
            ImageOverlay.Visibility = System.Windows.Visibility.Collapsed; 
            GrayOverlay.Visibility = System.Windows.Visibility.Collapsed;
            Loader.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer self employed
South Africa South Africa
I develop awesome Windows Phone/Windows 8 stuff, am a Nokia Developer Champion, I do Netduino electronics stuff, and blog a lot. I also occasionally do talks about development at Universities and conferences like TechEd. I run a small indie Windows Phone studio, currently working on an AppCampus-funded game.

Checkout my just-for-fun apps here: http://www.windowsphone.com/en-US/store/publishers?publisherId=RogueCode&appId=23d742d2-5b14-48a7-8e5f-b3b779537338
I also do Windows Phone (and Windows) development for clients, for example: http://www.windowsphone.com/en-za/store/app/dstv/a87feeed-a8dd-4bcb-8d47-15908340fdab

I am currently on hiatus from writing development articles for WPCentral.com.

My first book has just been published on home automation with a Netduino: http://www.amazon.co.uk/Netduino-Home-Automation-Projects-Cavanagh/dp/1849697825

Comments and Discussions