Page turner updated for Silverlight 2 RTM

by gleblanc 17. November 2008 21:57

Mitsu Furuta (from Microsoft France) updated his Page Turner to Silverlight 2 RTM. Let's explain how to use it.

Let's first demonstrate the page turner, with paintings from Pablo Picasso (the Spanish-born painter is currently honored in Paris with several outstanding exhibitions). Please note that some pages are more than just images.


Project / source code

You can find Mitsu's component at the following address : www.codeplex.com/wpfbookcontrol. Codeplex is really becoming a major player ! Download the zip-file and unzip it. You will find SLMitsuControls.dll (a 35 KB file!) in the SLMitsuControls/ClientBin folder. The component is under Ms-Pl licence, meaning you can use it in your own products, even commercial, for free (no royaltie to Microsoft, even if your product is not free) but, of course, you can't claim rights on ownership.

Let's now create a Silverlight application. Add a reference to SLMitsuControls.dll (in the Silverlight part of the project, right-click on the project, Add Reference and Browse to the folder containing the SLMitsuControls.dll file).

Let's first assume that our book-like control just contains images. These images need to be copied in the ClientBin folder (the one containing the XAP file). We first need to populate our book with these images. We keep file names (without extension) in an array of string (in Page.xaml.cs) :
 string[] ts = {"Picasso", "Head", "Blind man", "At the Lapin Agile", "Chicks from Avignon",
                "Woman crying", "Guitar player", "Auto portrait"};

In the UserControl tag (in Page.xaml), we add a xmlns tag for the control (any other name than local is OK), handle the Loaded event and insert our book in the first row of the main grid (the second row will contain a status bar implemented as a TextBlock) :
 <UserControl x:Class="TurnThePage2.Page"
    ..... 
    xmlns:local="clr-namespace:SLMitsuControls;assembly=SLMitsuControls"
    ..... 
    Loaded="UserControl_Loaded" >
  <Grid x:Name="LayoutRoot" Background="LightBlue" >
   <Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition Height="25" />
   </Grid.RowDefinitions>
   <local:UCBook x:Name="book" Grid.Row="0" OnPageTurned="book_OnPageTurned" Margin="20" />
   .....
  </Grid>
 </UserControl>
In the Page.xaml.cs file, we need to add a using, declare that the Page class implements the IDataProvider interface and implement two functions (just two small lines, always the same) :
 .....
 using SLMitsuControls;
 .....
 namespace TurnThePage2
 {
  public partial class Page : UserControl, IDataProvider
  {
   ..... 
   public int GetCount()
   {
    return pages.Count;
   }
   public object GetItem(int index)
   {
    return pages[index];
   }
  }
 }
In case our pages are just images, it's possible to keep them in a List<Image>. But some of our pages will be more elaborate (a page could for instance be a complex grid). So, we prefer List<UIElement> since UIElement is the base class for UI elements. But here, our book is (to begin) just a collection of images (only two images here, no need to show more) :
 List<UIElement> pages;
 .....
 private void UserControl_Loaded(object sender, RoutedEventArgs e)
 {
  pages = new List<UIElement> {
   new Image {Source=new BitmapImage(new Uri(ts[1]+".jpg",
              UriKind.Relative)), Stretch=Stretch.UniformToFill },
   new Image {Source=new BitmapImage(new Uri(ts[2]+".jpg", UriKind.Relative)),
              Stretch=Stretch.UniformToFill }
   };
  book.SetData(this);
 }
It's enough to get a page turner for images ! It's possible to handle the OnPageTurned event :
 private void book_OnPageTurned(int leftPageIndex, int rightPageIndex)
 {
  .....
 }
After a page turn, the arguments give you the left and right pages that are visible : starting at 1 (be careful !) and with -1 for absence of page.

Let's now complicate a bit : our first page will present an animated text on top of a photo and the last page will present a button on top of an image (remember : the images must be copied in the ClientBin folder). Our downloadable project is still a bit more complex (with a list box). We dynamically specify attributes for the grid in first page and dynamically add a TextBlock :  
 private void UserControl_Loaded(object sender, RoutedEventArgs e)
 {
  pages = new List {
            new Grid (),    // first page
            new Image ..... // second page (image)
            .....
            new Image ....
            new Grid()      // last page
           };
  // specify first page
  Grid g = pages[0] as Grid;
  g.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("Picasso.jpg", 
                                  UriKind.Relative)),
                                  Stretch = Stretch.UniformToFill }; 
  TextBlock tb = new TextBlock { Text = "Pablo\nPicasso", FontSize = 70,
                    Foreground = new SolidColorBrush(Color.FromArgb(255, 173, 216, 230)),
                    TextWrapping = TextWrapping.Wrap,
                    VerticalAlignment = VerticalAlignment.Center, 
                    HorizontalAlignment = HorizontalAlignment.Center,
                    TextAlignment = TextAlignment.Center };
  .....    // here the animation on FontSize
  g.Children.Add(tb);
  ......
The code to animate the FontSize property is :
 Storyboard stbPage0 = new Storyboard();
 Storyboard.SetTarget(stbPage0, tb);
 DoubleAnimation daSize = new DoubleAnimation {From = 70, To = 0,
                                RepeatBehavior = RepeatBehavior.Forever,
                                AutoReverse = true };
 daSize.Duration = new Duration(new TimeSpan(0, 0, 5));
 Storyboard.SetTargetProperty(daSize, new PropertyPath("FontSize"));
 stbPage0.Children.Add(daSize);  
 stbPage0.Begin();

The last page (with a button on top of an image) is similar :
 g = pages[pages.Count - 1] as Grid;
 g.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("Picasso2.jpg",
                                 UriKind.Relative)), Stretch = Stretch.UniformToFill };
 Button b = new Button { Content = "Back to the first page", Width = 130, Height = 50 };
 b.Click += new RoutedEventHandler(b_Click); 
 g.Children.Add(b);

The function that handles the click being, as usual :
 void b_Click(object sender, RoutedEventArgs e)
 {
  ..... 
 }
To go back to the first page, we just need to write :
 book.CurrentSheetIndex = 0;
It's possible to launch an animation to switch to a next or previous page : 
 book.AnimateToNextPage(1000);
 .....
 book.AnimateToPreviousPage(1000);
where the argument is the duration of the animation, expressed in milliseconds.

Tags:

Comments

11/19/2008 7:52:27 AM #

Trackback from Community Blogs

Silverlight Cream for November 18, 2008 -- #431

Community Blogs

11/21/2008 8:39:44 PM #


Awesome Gerard - thank you for sharing your hard work.

David Roh United States

11/25/2008 12:45:26 AM #

It is posible using this control to turn the page over one canvas. The idea of what I need to do is a book that when you open it has the pictures split in to parts. The firs page of the book would be the right side of the picture, and the second the left part. So the turn of the page would be from only one picture/canvas.

Nacho Uruguay

1/20/2009 8:49:10 AM #

you're idea is interesting and wonderful!!know i understand it thanks and more power!!it really helps me!!thanks again!

Busby SEO Test United States

1/24/2009 3:16:18 AM #

Trackback from DotNetShoutout

Gerard Leblanc on Silverlight 2 | Page turner updated for Silverlight 2 RTM

DotNetShoutout

2/14/2009 12:59:57 PM #

You have a very wonderful ideas, it really helps many people! Thanks to your effort and for sharing your great ideas..

Fix Windows Registry Republic of the Philippines

3/11/2009 1:48:13 PM #

thanks for the codes., I've been looking for it since months. i finally found it here., thanks.

do you suck at making money? United States

3/14/2009 2:09:09 PM #

Thanks for this great post, hope to read more of this.

cooldude055 United States

3/16/2009 12:38:31 PM #

im happy you make a post like this ,

Hits Tech Gadgets United States

4/13/2009 12:37:49 PM #

Thanks for that one.

goodpeoplegives United States

4/13/2009 1:06:17 PM #

Thanks for sharing this great post

cash4trends United States

4/13/2009 1:27:24 PM #

Thanks for the code.

billrainier United States

5/30/2009 3:16:58 PM #

The code is very nice. I have seen so many people include code in their posts that didn't make sense. I like how neat you write code.

Kmart EDI United States

7/14/2009 3:04:52 PM #

Do you earn decent money from this blog or are you doing it just for fun?

Tommy Bahama United States

8/5/2009 6:45:25 AM #

Thank you for your help!

payday loans United States

8/5/2009 6:45:28 AM #

Thank you for your help!

payday loans United States

8/11/2009 3:40:41 AM #

I usually don�t post in Blogs but your blog forced me to, amazing work.. beautiful �

cash loans United States

8/11/2009 3:40:44 AM #

Hello Guru, what entice you to post an article. This article was extremely interesting, especially since I was searching for thoughts on this subject last Thursday.

cash loans United States

9/4/2009 11:52:46 PM #

I think you made a great point. One that I have been thinking about as well.

Learn & Master Guitar United States

9/4/2009 11:52:51 PM #

Thanks, you cleared up some things for me.

Buying Selling Home United States

9/15/2009 12:45:24 AM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/15/2009 10:31:51 AM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/18/2009 10:13:29 PM #

Thanks for the wonderful concept and code.

I have a quick question. If I want to have a hyperlink that opens a new window for each of my page, is it possible? If so, will you provide some guidelines.

Thanks

Mahesh United States

9/21/2009 2:19:05 PM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/21/2009 11:43:06 PM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/23/2009 2:49:35 PM #

Thanks for providing valuable information on the topic. Keep posting

chat online United States

9/28/2009 4:35:39 PM #

I have never read such a lovely post and I am coming back tomorrow to continue reading.

web design United Kingdom

9/28/2009 4:44:39 PM #

I love your posting.

Russian translation United States

9/30/2009 11:33:17 AM #

It is posible using this control to turn the page over one canvas. The idea of what I need to do is a book that when you open it has the pictures split in to parts. The firs page of the book would be the right side of the picture, and the second the left part. So the turn of the page would be from only one picture/canvas.

Tiffany Rings People's Republic of China

10/2/2009 4:15:35 PM #

I wish getting over a broken heart can be so easy as following a few steps.. but its not� Frown

personal loan United States

10/2/2009 4:15:38 PM #

Thank you for your help!

personal loan United States

10/3/2009 7:10:47 PM #

Silver Slight it's great.

baby bouncer chairs United States

10/5/2009 8:48:40 AM #

Thanks for sharing this step by step guide. I'm sure that this post will help a lot of newbie(including me). Keep posting..

signature pad United States

10/7/2009 8:14:39 AM #

Thanks for your great post!!!

Ultimate Wealth Formula X United States

10/8/2009 10:29:37 PM #

I've not lost hope and I am planning to learn how to code now. Never is too late!

Ares download United States

10/9/2009 6:36:27 PM #

Thanks for providing such precious info.

Web Design United Kingdom

10/10/2009 1:17:20 AM #

You made some good points there. I did a search on the topic and found most people will agree with your blog.

cash loans United States

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Gerard Leblanc is the author of several books (in french) on C++, C#, .NET and Silverlight (Eyrolles, Paris as publisher). See www.gleblanc.eu as the companion web site for these books (included sample programs).
He is Microsoft MVP for Silverlight.

MVP logo