Book Image

Silverlight 4 User Interface Cookbook

By : Vibor Cipan (EUR)
Book Image

Silverlight 4 User Interface Cookbook

By: Vibor Cipan (EUR)

Overview of this book

Silverlight makes it much easier to build web applications with highly usable, interactive, and exciting user interfaces. However, with so many new options open to designers and developers, making the best use of the tools available is not always so easy. It's ease of use and rapid development process has left one area completely uncovered— how to design, build, and implement professional and usable interfaces, and create an enjoyable user experience and interaction. Written by a Microsoft MVP and Silverlight Prototyping Specialist, this book is the first and only book on developing Silverlight User Interfaces. Clear, step-by-step instructions show how to build all the user interface elements that users look forward to in a cutting edge app. This book offers essential recipes, with each recipe depicting the commonly used user interface patterns built with Silverlight, and in some cases, with WPF to showcase the possibilities. The author's experience in designing and developing user interfaces enables him to share insights on creating professional interfaces in a clear and friendly way. The book starts off with recipes dealing with fixed and fluid layouts, building custom command link controls, working with navigation, and collapsible panels, and then moves on to the more advanced topics such as calendars, alternating row colors, and task panes. The author covers a number of different UI patterns, controls, and approaches accompanied by XAML and C# code where needed (and explained), along with usage context and practical, proven, and professional techniques for specific controls and patterns. From maps to task panes, and web cam support to pixel shaders, this Cookbook provides you with a rich selection of Silverlight UI recipes. It covers all that you need to know in order to design and implement a user interface, together with professional user experience and interface guidelines to make your solutions and applications pleasurable for your users.The author has found himself in the role of both, a designer and a developer, at different points in his professional career, and his motive was to create a book that will serve as a useful resource for designers and developers trying to find their way with Silverlight and Expression Blend.By the end of the book, you will be able to create a rich, professional, and standards-compliant user interface.
Table of Contents (13 chapters)
Silverlight 4 User Interface Cookbook
Credits
Foreword
About the Author
Acknowledgement
About the Reviewers
Preface

Fluid layout


There are two basic types of layouts that I want to consider here. First is the "fixed" layout, which basically means that all of the elements on your page or window will remain of the same size, irrespective of the screen resolution or other form factor. Then, there is "fluid" layout, which is good for enabling your content to adjust in a size that is appropriate for the current screen resolution, page dimensions, or generally the form factor that your users will be using.

I am going to show you how to create a simple example of fixed and fluid layout in Silverlight. After that, I will give you some guidance on when to use fixed and when to use fluid layouts.

Getting ready

Start your Expression Blend 4 and then select New project... From the dialog that appears select Silverlight and then Silverlight Application, make sure that Language is set to C# and Version is 4.0. At the end hit OK.

Note

Some recipes in this and the following chapter are dealing with WPF, not with Silverlight. However, ideas, methods, approaches and user experience guidelines are applicable to both technologies.

Now we will see how to create a fluid layout design.

How to do it...

We will create two examples: the first one will demonstrate fluid layouts and the second one, fixed layouts.

  1. 1. After you have created your new project, under the Objects and Timeline pane, you will see UserControl and LayoutRoot. LayoutRoot is a grid control hosted in UserControl.

  2. 2. Click on UserControl and change its height and width to Auto. To do that, go to Properties | Layout and change these properties. You will notice that they are set to 640 and 480 by default. Click on the little cross-arrows to set them to Auto.

  3. 3. Now, let's change the background color of LayoutRoot. Click on it in the Objects and Timeline pane, then go to Properties pane, navigate to the Brushes section, click on Background, select Solid color brush (second icon in the row), and select any color.

  1. 4. Press F5 or go to Project | Test Solution.

  2. 5. Your default web browser will start and you will see that the entire surface is covered in the color that you have previously set for your UserControl. Try resizing the browser and you will see that the surface is still completely covered with the selected color.

  3. 6. Close your browser and return to Blend.

  4. 7. Now we will add some objects on top of LayoutGrid and explore their behavior.

  5. 8. Change the design-time size of your UserControl by clicking and dragging handlers. Be sure to select UserControl before you start resizing. Note that this is changing only the design-time dimensions; run-time dimensions are still set to Auto.

  1. 9. On the toolbox (usually a left aligned, vertical stripe with a number of different controls and tools), locate and select the Rectangle tool. You can also select it by pressing the M key. Draw several rectangles on top of LayoutRoot.

  1. 10. Again, press F5 to start your project.

  2. 11. Play with your web browser now. Try changing the height and width and notice the behavior of the rectangles; they will also change their dimensions. You don't need to think about the mechanics behind this behavior right now.

  3. 12. Close your browser and return to Blend again.

How it works...

How does it really work? The first step was changing the width and height properties of our UserControl to Auto. By doing so, we have allowed it to stretch automatically and fill in all available space. Simple as that! As our LayoutRoot is Grid control and its height and width properties have been set to Auto as well, LayoutRoot has also filled in all available space. We have just changed its color to make it more distinguishable. That is why our browser has been filled with LayoutRoot.

After that, we added several rectangles to our LayoutRoot. And here is where some interesting behaviors start. Depending on the position of rectangles, some of them have been aligned horizontally or vertically to different sides of LayoutGrid. You can easily check that by clicking on them and then looking at HorizontalAlignment and VerticalAlignment under the Layout section within the Properties pane. Depending on that alignment, rectangles have been resizing when you have resized your browser. This was a very basic illustration of a liquid type of layout. We implement such a layout by using the grid control. Next, we will see how to make a fixed layout design.

How to do it...

The steps are the same as those of the previous example the creation of liquid fluid layout. I will assume that you have followed those directions and will take it from there.

So, the main difference will be that instead of using Grid for LayoutRoot, we will use Canvas.

  1. 1. Right-click on LayoutRoot in the Objects and Timeline pane and from the menu, select Change Layout Type | Canvas.

  2. 2. Hit F5 to test your project.

  3. 3. Try resizing your browser again. Notice that the rectangles are keeping their positions as well as their dimensions intact. No matter what you do with your web browser size, they will be the same.

How it works...

What happened after we changed our Grid control to Canvas type of control in the second example? All our rectangles have retained their size and positions, no matter what we have done with browser. The reason lies in the fact that Canvas control used the so-called "absolute positioning" and no layout policy is being applied to its child elements (rectangles in our case). You can literally consider it as a blank canvas.

There's more...

To summarize, if you require maximum layout flexibility, you will use Grid control. It employs a number of different rules that can be applied to its child elements. For fixed layouts, which are not dependent on the screen size or resolutions, you will use Canvas control with absolute positioning.

More info about grid sizing, rows, and columns

The grid is a truly versatile control and it enables you to build very flexible and sometimes complex layouts.

A really important concept includes the possibility of dividing the grid into rows and columns, so that you get even more flexibility for your layouts. With grid control selected, position your mouse pointer over its border. You will notice that the mouse pointer will change its appearance and look like the following screenshot. You can click to add gridlines and define columns and rows in that way.

Now it is important to understand that the grid supports three ways of defining sizes for columns and rows fixed, star, and auto.

Fixed sizing uses exact pixel values to define row or column dimensions and means that they will not resize. Star sizing is using relative dimensions. In fact, it just indicates that width should be relative to the other star-sized columns (or rows, for that matter).

Consider the following examples:

<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="1000*"/>
<ColumnDefinition Width="1000*"/>

These three definitions will have the same column width. They will be identical with each column occupying the same amount of space.

Now, consider this sample:

<ColumnDefinition Width="1000*"/>
<ColumnDefinition Width="2000*"/>

Here, the second column will be exactly two times bigger than the first one a ratio of 2:1.

Auto sizing means that the elements contained in a grid will also resize when the parent element resizes. It also means that the size of that column or row will depend on the size of the object that is located in that specific column or row.

In XAML code, those definitions can look like this:

<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="69"/>
<ColumnDefinition Width="0.52*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="69"/>
</Grid.ColumnDefinitions>
</Grid>

If you omit the width definition, Blend will automatically use star sizing.

More detailed information about Grid and Canvas panel controls can be found in the Microsoft Expression Blend User Guide. Just hit F1 to access it within your application.

When to use fixed and when to use fluid layouts

As you might have guessed already, there is no definite suggestion as to whether you should use only one of those layout types. Most likely, you will be using both of them. For example, message boxes or some dialogs will not benefit from resizing, but general application workspaces and web pages might want to look into using the most or all the space available to them in a meaningful manner. Also, if you need to preserve some fixed positions, you will use a fixed layout system.

However, there are some proven practices outlining the usage of fixed and fluid layouts.

Fixed layout works well for message boxes displaying information or errors to end users. They will not benefit significantly by adding the ability to resize them. Same goes for dialog boxes presenting users with progress on operations such as file copying or deleting. Keep their sizes fixed. Of course, you will ensure that they look good at different screen resolutions by testing them. You can even try to optimize content positioning, depending on the current screen resolution, but you should not allow users to resize those types of windows. If you consider web environments (you can apply the same principles here), all those modal windows, pop-up dialogs, and message boxes should be of a fixed size.

When it comes to fluid layouts, the situation is more complex and it is more challenging to give design guidelines. But generally, you should use liquid layout type in cases where your application and dialog boxes can benefit from more space available to them. If you are building a file browser, you should implement the "liquid layout" system and use all available space to show as many files and folders as possible, so that your users can select them without scrolling too much. The same goes for web pages in general, you would want to use as much space as possible for your content. But you should be aware that there are limits. You can put your textual content on the web page in liquid layout and enable it to resize together with your web browser and use the extra space available to them. But from the usability point of view, lines of text that are too long will decrease readability dramatically. Today, screen sizes are often over 20 inches and the trend of increasing sizes will continue into the future for some time does that mean that you should enable your text or controls to scale accordingly? Absolutely not! The general rule is that your line of text should not be longer than about 66 characters. The reason for that is the fact that the human eye struggles with reading lines longer than that. I recall seeing that suggestion in The Elements of Typographic Style written by Canadian typographer, Robert Bringhurst. If you apply some good fonts and think about the overall appearance, you can go up to 75 characters, but don't go over that.

What I like to do is define three sizes for my UI elements: preferred (which is basically default or optimal) size, minimum size, and maximum size. Preferred size is the size that the specific UI element will have on a given screen resolution. Minimum size is the size that will be the smallest size each element can scale to. For example, you can choose to set the minimum size of your window to 800 by 600 pixels and optimize all controls within it to fit into that. Also, by setting maximum size you can ensure that your form and controls will not stretch too much (as I suggested a moment ago when talking about text length). The great thing is that WPF and Silverlight also support these sizes and you can set them for each control you add to your UI.

Other suggestions for dealing with controls in liquid layouts include keeping navigation elements (in web pages) anchored to top and left margins. Controls such as text boxes, tables, grids, and list views should increase their lengths and possibly even widths but again, recall what I have said in the previous paragraph about too long text lines same applies to all controls. If you are using background colors, patterns, or similar elements, they should fill the new space completely and that is the only case where you should use all space available.

It is also important to think about what will happen if your window or web page gets too small for its content. As I said earlier, set the minimum size for each and every control including container controls such as windows or pages. Optimize the layout for that case. If you don't do that, your controls might become inaccessible during run-time, aesthetic dimension will be also destroyed, and your application will become practically useless or extremely hard to use.

Most WPF and Silverlight controls will adjust nicely by adding scrollbars to help adapt to these changes, so that will spare you from implementing all that logic by yourself.

Don't be afraid to put some constraints on liquid layouts, sometimes too much flexibility will harm your user's experience though your intentions were quite the opposite. I am a firm believer that by limiting certain aspects of UI flexibility we are actually helping our users and building better experiences.

See also

  • Control docking with DockPanel