How to switch Silverlight UserControls

23 thoughts on “How to switch Silverlight UserControls”

  1. You might want to add the following piece of code to your post to document how to make this run:

    private void Application_Startup(object sender, StartupEventArgs e)
    // Load the main control

    this.RootVisual = new UserControlContainer();
    Page p = new Page();

  2. This was very helpful to me. Just the simple and elegant page switching approach I needed.

  3. How to hold the state of page when switch back
    from testControl

  4. Great post, how do I update switch the control if I want to update the Silverlight Control from the hosting web page? I have a .aspx page and based on a selection that a user makes, I want to change the Silverlight control to be displayed. However in the .aspx.cs I don’t have access to the UserControlContainer.

  5. I did this using startup params.
    The “flow” goes something like this :

    1. User requests page so server builds page
    2. server sends page to client and client scripts execute
    3. silverlight objects are executed

    So …

    In your aspx page html you put the standard object tag with this in it :

    depending on requirements you can either edit this object tag on the server or the client.

    1 Of the params I pass in is a “StartingControl” which in the App.Xaml.cs file I do this with :

    void Application_Startup(object sender, StartupEventArgs e)
    case “Control1” :
    this.RootVisual = new Control1();

    This gives you switching functionality before the silverlight object is loaded.

    Combine the 2 samples and you can make whatever changes you need at any point.


  6. In an application with – say 1000 – pages, do all of these pages get streamed to the client (browser), or just the currently visible page?

  7. Well if by “pages” you mean Silverlight canvases/controls, then yes, they all get packaged in your XAP file and sent to your browser at the same time. Using Silverlight 3 though you can do some more interesting deferred loading of resources.

  8. The InitParams solution is good for views that only change when the web page is refreshed. My solution is for when you need to keep a long-runnning instance of the same control.

  9. It depends how you implement your concept of a user. If you are using the commonly used cookie-based forms authentication scheme then you could make a Web service call to a logout/login method and change user that way.

  10. Oh my god that is the most useful thing Ive ever found on
    the internet!! Thankyou. Your neat little fix just made my
    coursework work!!

  11. How about just clear and add the children collection
    directly in the transfer event like private void
    cmdSLControl_Click(object sender, RoutedEventArgs e) {
    LayoutRoot.Children.Clear(); LayoutRoot.Children.Add(new
    SilverlightControls()); }

  12. Just call this function with required page u want load…. dats it

    public void SwitchControl(UserControl newControl)



  13. This is a useful post.Can you explain me switching rootvisuals between silverlight test project and silverlight3 main project.If i try this i get an error
    Unable to cast object of type ‘Microsoft.Silverlight.Testing.UnitTesting.UI.TestPage’ to type ‘’.

Comments are closed.