Page Switching With Parameters (the short version)

 

I recently finished Part 2 of Page Switching in which I demonstrate how to pass parameters between Silverlight pages (this video is in production and should be available soon.)

A reader asked if I could provide the essential information, so here it is, very short, and very terse, I’m afraid.

The trick, such as it is, is to overload the Navigate method in the PageSwitcher user control.  Here’s the code from PageSwitcher.xaml.cs

public partial class PageSwitcher : UserControl
{
   public object ObjectValue { get; set; }

   public PageSwitcher()
   {
      ObjectValue = null;
      InitializeComponent();
      if ( this.Content == null )
      {
         this.Content = new Page();
      }
   }

   public void Navigate( UserControl nextPage )
   {
      this.Content = nextPage;
   }


   public void Navigate( UserControl nextPage, object value )
   {
      this.ObjectValue = value;
      this.Content = nextPage;
   }

}

The overloaded Navigate takes an object and stores it in the PageSwitcher’s member variable ObjectValue. The page you switch to then only has to retrieve it.

The call to this overloaded method is demonstrated by creating an object of type person (which in this simplified version has three member variables, FirstName, LastName and a boolean as to whether the person is a member of the club)

 

void Switch_Click( object sender, RoutedEventArgs e )
{
   Person p = new Person();
   p.FirstName = FirstName.Text;
   p.LastName = LastName.Text;
   p.IsMember = RBYes.IsChecked == true;
   PageSwitcher ps = this.Parent as PageSwitcher;
   ps.Navigate( new PageX(), p );
}

 

p is an instance of person, we pass in to Navigate the new instance of PageX and the new instance of the Person object. 

In PageX’s PageLoaded method we look to see if the PageSwitcher’s object is not null, and if not, we cast it to be a person and then we can extract the values…

void Page_Loaded( object sender, RoutedEventArgs e )
{
   PageSwitcher ps = this.Parent as PageSwitcher;
   object theObject = ps.ObjectValue;
   if ( theObject != null )
   {
      Person p = theObject as Person;
if ( p != null )
{

Message.Text = "Found " + p.FirstName + " " + p.LastName + ( p.IsMember ? " is a member!" : " is not a member yet." );

} // end cast is okay } // end object is not null } // end Page_Loaded

 

Message in this case is a TextBlock that displays the output.  

The last bit of of code uses the conditional operator, you read it as “if p.IsMember evaluates true, return the string ‘ is a member ‘ otherwise return the string ‘is not a member yet. ‘

The video goes though this in greater detail, but that is the essence.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.

3 Responses to Page Switching With Parameters (the short version)

  1. storify.com says:

    Everything iis very open with a clear explanation of thhe challenges.
    It was definitely informative. Your site is extremely helpful.

    Thank you for sharing!

  2. fast money says:

    Heya i’m for the first time here. I came across this board
    and I finhd It truly useful & it helped me ouut much.
    I hope to give something back and help others like
    yyou helped me.

    Look into my web-site :: fast money

  3. Fumi says:

    hi, this article is very nice! I could find lots of website about using url…

    thank you for uploading.

Comments are closed.