Page view counter

Web Cast and Digging Deeper

Today I had the pleasure of presenting a web cast on building an application with more than one page and passing data among those pages. I have 2 videos on this topic, so if you missed the web cast and are interested, you may find this first video on switching pages of interest, and the second video should be posted soon.

TwoPages

 

Multi-Page In A Nutshell

The premise is that there is a third page that has no content and when it is time to display one of your pages, you make the page you want to display (which is, after all, a UserControl) the content of that third page (I’ve chosen to name the third page PageSwitcher but of course you may call it anything you like. PageSwitcher has a method, Navigate that takes a UserControl and makes whatever you pass in, its content.  The implementation for the button on each page is to make the page you are switching to the new content.

Passing Values

Because we destroy the current page when we replace it (to conserve on client-side resources) if we wish to pass information (such as the book name, authors, whether the book is published and whether or not it is fiction) the easiest way is to make PageSwitcher the temporary repository; and the most efficient way to do that is to overload the navigate method to take an Object so that you may pass in anything at all, and then fish it out when your new page is loaded (very effective, very efficient and not at all type safe!).

To see this at work, assume the user has filled out the fields as shown in Page 1 and clicked switch. The event handler for the button might look a bit like this:

void SwitchButton_Click( object sender, RoutedEventArgs e )
{
   PageSwitcher ps = this.Parent as PageSwitcher;
   if ( ps != null )
   {
      Book b = new Book();
      b.BookName = BookName.Text;
      b.Authors = Authors.Text;
      b.isPublished = IsPublished.IsChecked == true;
      b.isFiction = Fiction.IsChecked == true;
      ps.Navigate( new Page2(),b );
   }

We know that our “parent” is the PageSwitcher as we are its content, and so the cast is safe (but being good o-o programmers we check (Доверяй, но проверяй)

We then spin up a Book instance (defined off camera and fill in the properties from the user-filled in fields and then call the overloaded Navigate method, providing a new instance of Page2 and our newly filled in book.

We trust PageSwitcher to (a) abandon Page.xaml and make Page2 its content and to hold onto whatever we’ve passed it until Page2 asks for it (think Secretkeeper in Harry Potter).

When Page2 is safely established, it asks PageSwitcher if there are any little presents waiting, like this:

void Page2_Loaded( object sender, RoutedEventArgs e )
{
   PageSwitcher ps = this.Parent as PageSwitcher;
   if ( ps != null )
   {
      if ( ps.SwitcherTag != null )
      {
         Book b = ps.SwitcherTag as Book; 
         
         string isPublished = string.Empty;
         if ( b.isPublished == false)
         {
            isPublished = " not ";
         }
         string isFiction = b.isFiction ? " is Fiction " : "is non-fiction";

         BodyText.Text = "Found " + b.BookName + " written by " +
            b.Authors + ". The book is " + isPublished + " published and " + isFiction;
      }
      SwitchButton.Click += new RoutedEventHandler( SwitchButton_Click );
   }

That is, if you are my parent (and you are) do you have anything tucked away in your SwitcherTag property. (“Whats it gots in its pocketses?”) If so, I happen to know that it is a really a book object, and it is my book object and gimme.

Now all of that is fine and good but we’ve been here before. One of the viewers asked if I could show how to pick among 4 pages, and by the way could my buttons look cooler. So, instead of working on my chapter, which I really have to do, I spent the past four hours creating a new version that has bouncy buttons with images that take you to four different pages.

It’s wicked easy to do if (and only if) you are willing to make 4 templates. If you want one template, well then my friend you have to figure out how to expose not only the content property that button has, but the image source property that button doesn’t have, and the short answer is you can’t unless you are willing to sub-class, and that means a custom control and that is a lesson for another day (quite soon actually – I have a WebCast coming on that on August 27

So, compromising and creating 4 templated buttons (yuck but what can I say?) I did make them bounce (they look a bit like they want to be on the Mac dock but don’t tell anyone) I do have an application that combines Skinning and multiple pages, and transferring data and in-state animation which I will turn into a video because, frankly, it is too late at night to continue that story here.

But here’s a picture (doesn’t bounce much and the first button is disabled)

BouncingButtons

Stay tuned for the video walkthrough and then for the real-deal: skinnable custom controls (after which we can re-do this with one template).

thanks again.

-j

 

PS: the two books shown are highly recommended, but have nothing to do with the topic at hand.

Look Both Ways: Bisexual Politics
by Jennifer Baumgardner
Read more about this title...
Pillar of Fire : America in the King Years 1963-65 (America in the King Years)
by Taylor Branch

Read more about this title...
Published Wednesday, August 06, 2008 11:25 PM by jesseliberty
Filed under:

Comments

# re: Web Cast and Digging Deeper

Hi Jesse,

       Nice article about Multi-Page.But i have few concerns.

Basically i am going to develop a SL based Web App which is data driven.Although i am planning to use Silverlight-ViewModel-Pattern, now using www.nikhilk.net/Silverlight-ViewModel-Pattern.aspx

of Nikhil.

Concerns:

  1 : How can i make Header, Footer,Navigation etc.

  2 : Communication b/w pages.

  3 : Any suggested architecture.

  4 : Will Nikhel's Framework help me in this regards.

Thursday, August 07, 2008 8:17 AM by forniyaz

# Silverlight news for August 7, 2008

Pingback from  Silverlight news for August 7, 2008

Thursday, August 07, 2008 10:13 AM by Silverlight news for August 7, 2008

# re: Web Cast and Digging Deeper

In response to your concerns about header, footer, etc. You certainly can do all that with Silverlight, using user controls and custom controls, but I would seriously take a look at whether you need a client-side only solution or it might make sense to create an ASP.NET application with Silverlight handlingg all the interactive parts. That might be easier, faster to develop, easier to maintain and deploy.

If not, I think you're in for a pretty complex design, with pages whose job it is to act as switches and "master" pages and to pass data from one page to another, and of course you'll still have to go back to servers one way or another for data (web services?).

Thursday, August 07, 2008 3:00 PM by jesseliberty

# re: Web Cast and Digging Deeper

Hi Jesse,

Thanks for quick response, thanks again.

Can we implement Master Page concept in SL.?

I am have to use SL due to my client, no ASP.NET nor HTML controls etc.?

How i can do Session Management in SL.?

To deploy SL on server what should be there on the server.?

Waiting for kind response again.Thanks in advance.

Regards,

Muhammad Niaz

Friday, August 08, 2008 12:59 AM by forniyaz

# Silverlight Cream for August 09, 2008 - 2 -- #342

Michael Washington released the latest Silverlight Desktop, Anna Wrochna has three good short SL posts

Sunday, August 10, 2008 12:48 AM by Community Blogs

# re: Web Cast and Digging Deeper

Hi Jesse,

I just posted a question in the forums the other day asking if there was a way to reuse a control template for a button (very similar to yours here) where I could pass in both the text content and the image source. I was afraid it was going to require a custom control, but I was hoping that wasn't the answer. :-) I guess I'll be looking forward to your upcoming webcast. It would be nice if there were a way to pass a parameter into a control template to avoid having to do that though.

Kevin

Monday, August 11, 2008 1:17 PM by SixBuckeyes

# Recent Links Tagged With "page2" - JabberTags

Pingback from  Recent Links Tagged With "page2" - JabberTags

Friday, May 15, 2009 5:49 PM by Recent Links Tagged With "page2" - JabberTags