Windows Presentation Foundation Tutorial 1



Similar documents
Windows Presentation Foundation

How to create a Flash banner advert in DrawPlus X2

Microsoft Virtual Labs. Building Windows Presentation Foundation Applications - C# - Part 1

Unit 21 - Creating a Button in Macromedia Flash

Relationships in WPF Applications

Hands-On Lab. Building a Data-Driven Master/Detail Business Form using Visual Studio Lab version: Last updated: 12/10/2010.

Working With Animation: Introduction to Flash

Tutorial. Introduction to Windows Movie Maker 2.1. A Hands-on Workshop. from the Academic Technology Center. Distributed Learning Services, CIT

Creating and Using Links and Bookmarks in PDF Documents

Understanding In and Out of XAML in WPF

Flash MX Image Animation

Microsoft Surface Lab #2: Surface Controls

Using Microsoft Word. Working With Objects

Integrating Educational Technology into Teaching (4 th Edition) Adobe Acrobat Tutorial for Chapter 15 TIE-into Practice Exercises

Quick Start Guide Simple steps for editing and manipulating your photo.

EDIT202 PowerPoint Lab Assignment Guidelines

PowerPoint 2007: Animations Contents

Creating a Calendar in CorelDRAW

WPF Shapes. WPF Shapes, Canvas, Dialogs 1

Introduction to C#, Visual Studio and Windows Presentation Foundation

Movie Maker 2 Beginning

ComponentOne. Windows for WPF

Visual Studio 2008: Windows Presentation Foundation

Creating forms in Microsoft Access 2007

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Windows 10 Apps Development

Joomla Article Advanced Topics: Table Layouts

Creating a Form. A form is something that is created allowing users to enter information in a more visual manner than a datasheet view.

Course Project Lab 3 - Creating a Logo (Illustrator)

Instructions for Formatting APA Style Papers in Microsoft Word 2010

Development. Wiley Publishing, Inc. 24-HOUR TRAINER. Brian Faucher WILEY

The very basic basics of PowerPoint XP

CREATE AN ANIMATED AQUARIUM IN POWERPOINT

Click on various options: Publications by Wizard Publications by Design Blank Publication

Welcome to CorelDRAW, a comprehensive vector-based drawing and graphic-design program for the graphics professional.

Introduction To Microsoft Office PowerPoint Bob Booth July 2008 AP-PPT5

The VB development environment

Animating in Inventor Studio the basics

Motion tween is nothing but tweening a Symbol's movement from one position to another.

Tutorial Overview Quick Tips on Using the ONRR Statistical Information Website

Education Solutions Development, Inc. APECS Navigation: Business Systems Getting Started Reference Guide

WebPlus X8. Quick Start Guide. Simple steps for designing your site and getting it online.

How to make a custom Joomla template!

Adobe Illustrator CS6. Illustrating Innovative Web Design

Basic tutorial for Dreamweaver CS5

Jump-Start Tutorial For ProcessModel

To Begin Customize Office

Information Server Documentation SIMATIC. Information Server V8.0 Update 1 Information Server Documentation. Introduction 1. Web application basics 2

ios 9 Accessibility Switch Control - The Missing User Guide Updated 09/15/15

Setting up Auto Import/Export for Version 7


Snagit 10. Getting Started Guide. March TechSmith Corporation. All rights reserved.

Pro/E Design Animation Tutorial*

Fireworks CS4 Tutorial Part 1: Intro

Name of chapter & details

This tutorial has been designed for all those readers who want to learn WPF and to apply it instantaneously in different type of applications.

Windows Live Movie Maker Tutorial

About XML in InDesign

Microsoft Office PowerPoint Identify components of the PowerPoint window. Tutorial 1 Creating a Presentation

Recipes4Success. Animate a Rocket Ship. Frames 6 - Drawing Tools

File Folders. To produce a file folder you have the following options:

Section 1: Ribbon Customization

Using WINK to create custom animated tutorials

Microsoft Windows Movie Maker

The following is an overview of lessons included in the tutorial.

INTRODUCTION TO DESKTOP PUBLISHING

Action settings and interactivity

Introduction to Microsoft Word 2003

Microsoft Word: Moodle Quiz Template

LEARNING RESOURCE CENTRE. Guide to Microsoft Office Online and One Drive

Geocortex HTML 5 Viewer Manual

Draw pie charts in Excel

HOW TO MAKE A TABLE OF CONTENTS

Getting Started Guide

How to Create a Fun Circus Tent Icon. Final Image Preview. Tutorial Details. Step 1. By: Andrei Marius

What is Microsoft PowerPoint?

Screencast-o-matic ProPage Basics

Tutorial 13: Object Animation

Introduction to Application Development with Silverlight for Windows Embedded. Abstract. Windows Embedded CE 6.0 R3 Technical Article

Using Microsoft Access

GYM PLANNER. User Guide. Copyright Powerzone. All Rights Reserved. Software & User Guide produced by Sharp Horizon.

OPERATION MANUAL. MV-410RGB Layout Editor. Version 2.1- higher

Payroll Import. Version Main screen for Payroll Import.

Microsoft PowerPoint 2007

Microsoft Visual Studio 2010 Instructions For C Programs

Windows Presentation Foundation (WPF) User Interfaces

paragraph(s). The bottom mark is for all following lines in that paragraph. The rectangle below the marks moves both marks at the same time.

CREATING FORMAL REPORT. using MICROSOFT WORD. and EXCEL

Structuring your essays, theses and reports using Word 2011 for Mac

SQL Server 2005: Report Builder

Petrel TIPS&TRICKS from SCM

Silverlight for Windows Embedded Graphics and Rendering Pipeline 1

Auto Clicker Tutorial

Microsoft Word Quick Reference Guide. Union Institute & University

TABLE OF CONTENTS SURUDESIGNER YEARBOOK TUTORIAL. IMPORTANT: How to search this Tutorial for the exact topic you need.

Creating a Flyer with Word

Customising Your Mobile Payment Pages

Table of Contents. I. Banner Design Studio Overview II. Banner Creation Methods III. User Interface... 8

8 CREATING FORM WITH FORM WIZARD AND FORM DESIGNER

Transcription:

Windows Presentation Foundation Tutorial 1 PDF: Tutorial: A http://billdotnet.com/wpf.pdf http://billdotnet.com/dotnet_lecture/dotnet_lecture.htm Introduction 1. Start Visual Studio 2008, and select a WPF Application. Next add the following highlighted XAML code: <Window x:class="wpfapplication1.window1" Title="Window1" Height="304" Width="466"> <Border BorderBrush="Black" BorderThickness="5" Margin="10" Padding="5"> <Button>First button</button> </Border> 2. Next try the following colours for the Border: AliceBlue, CornflowerBlue and MediumBlue. 3. Next try to change the colour of the text on the button to AliceBlue. 4. Change the name of the form to Napier. B Exporting and importing XAML 5. Install Microsoft Expression Design (Figure 1), and start the package, and create the Scottish flag (see below). Once complete export it to c:\scotland.xaml. Figure 1: Microsoft Expression Design W.Buchanan 1

Note: if you cannot install with package on your computer, you can download the exported XML from: http://billdotnet.com/scotland.zip 6. Next import the XAML into the form (such as in the highlighted text). Figure 2 shows the result of a sample run. <Window x:class="wpfapplication1.window1" Title="Napier" Height="304" Width="466"> <Button> <Canvas xmlns:c="http://schemas.openxmlformats.org/markup-compatibility/2006" compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2006" c:ignorable="d" x:name="untitled1" Width="800" Height="600"> <Canvas x:name="layer_1" d:islayer="true" Width="800" Height="600" Canvas.Left="0" Canvas.Top="0"> <Rectangle x:name="rectangle" Width="288" Height="197" Canvas.Left="74" Canvas.Top="27" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FF230FD2"/> <Path x:name="line" Width="309" Height="216" Canvas.Left="64" Canvas.Top="17" Stretch="Fill" StrokeThickness="30" StrokeLineJoin="Round" Stroke="#FFFFFFFF" Data="F1 M 70,116L 391.333,333.333"/> <Path x:name="line_0" Width="288" Height="197" Canvas.Left="74" Canvas.Top="27" Stretch="Fill" StrokeThickness="30" StrokeLineJoin="Round" Stroke="#FFFFFFFF" Data="F1 M 390,110.667L 70,329.333"/> </Canvas> </Canvas> </Button> Figure 2: Example button W.Buchanan 2

C Assigning an event to a button 7. Next modify the button code so that it now has an event, such as with: <Button Click="ButtonOkClicked"> and go to the Window1.xaml.cs file, and add: private void ButtonOkClicked(object sender, RoutedEventArgs e) { } MessageBox.Show("Hello"); 8. Run the project, and show that you can click on the button and that it displays a message box. 9. Add another button from the toolbox, and define that this is an exit button with the code: private void button1_click(object sender, RoutedEventArgs e) { Close(); } D UI Layout 9. Next create a ListBox with the following highlighted text: <Window x:class="wpfapplication1.window1" Title="Napier" Height="304" Width="466"> <StackPanel> <TextBlock>My Menu</TextBlock> <ListBox> <ListBoxItem>Napier University</ListBoxItem> <ListBoxItem>Centre for Distributed Computing and Security</ListBoxItem> </ListBox> <RichTextBox/> </StackPanel> 10. Update the ListBox tags with the events: <ListBoxItem Selected="Web1">Napier University</ListBoxItem> <ListBoxItem Selected="Web2">Centre for Distributed Computing and Security </ListBoxItem> W.Buchanan 3

11. Next add the events to the CS file: private void Web1(object sender, RoutedEventArgs e) { Process.Start("IExplore.exe", "http://www.napier.ac.uk"); } private void Web2(object sender, RoutedEventArgs e) { Process.Start("IExplore.exe", "http://www.cdcs.soc.napier.ac.uk/"); } Note: make sure you add: using System.Diagnostics; 12. Run the program and make sure it works (see Figure 3). Figure 3: Example button 13. Now add a ListBoxItem to exit the program. D Dockpanel Layout 14. Now we can create a text box on the left hand side, and the menu on the left with: <Window x:class="wpfapplication1.window1" Title="Napier" Height="304" Width="466"> <DockPanel> <TextBlock DockPanel.Dock="Top">Napier</TextBlock> <ListBox DockPanel.Dock="Right"> <ListBoxItem>Main Site</ListBoxItem> <ListBoxItem>CDCS</ListBoxItem> </ListBox> W.Buchanan 4

<RichTextBox/> </DockPanel> 15. Modify this so that it contains the links from the previous example. 16. The menu can be on the other side with: <Window x:class="wpfapplication2.window1" Title="Window1" Height="300" Width="300"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="3*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Grid.ColumnSpan="2">My UI</TextBlock> <ListBox Background="Aqua" Grid.Row="1" Grid.Column="0"> <ListBoxItem>Napier University</ListBoxItem> <ListBoxItem>Centre for Distributed Computing and Security</ListBoxItem> </ListBox> <RichTextBox Grid.Row="1" Grid.Column="1"/> E Data binding 17. Create a new project with a Windows-based WPF application, and copy the code from the following. In this case it has the name of two of the text boxes (txtf and txts): <Window x:class="wpfapplication2.window1" Title="Window1" Height="304" Width="373"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> on/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> W.Buchanan 5

<ColumnDefinition Width="75"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label>First Name:</Label> <TextBox Grid.Column="1" x:name="txtf" /> <Label Grid.Row="1">Last Name:</Label> <TextBox Grid.Row="1" x:name="txtl" Grid.Column="1" /> <Label Grid.Row="2">Street:</Label> <TextBox Grid.Row="2" Grid.Column="1" /> <Label Grid.Row="3">City:</Label> <TextBox Grid.Row="3" Grid.Column="1" /> <Label Grid.Row="4">State:</Label> <TextBox Grid.Row="4" Grid.Column="1" /> <Label Grid.Row="5">State:</Label> <TextBox Grid.Row="5" Grid.Column="1" /> <Button HorizontalAlignment="Right" Grid.Row="4" Width="75" Grid.Column="1" Click="Button_Click">Save</Button> 18. Next update the code for the event on the button with: private void Button_Click(object sender, RoutedEventArgs e) { string firstname = this.txtf.text; string surname = this.txtl.text; MessageBox.Show("Your name is " + firstname + " " + surname); } 19. Figure 4 shows a sample run. Modify the XAML and C# code so that it shows the three other values in the message box. Figure 4: Getting values W.Buchanan 6

F Template standards 20. In XAML it is possible to define new style tags. Add the highlighted code in the following example, and prove that it defines a new style. Update it so that it uses Times New Roman with non-bold text. <Window x:class="wpfapplication2.window1" Title="Napier" Height="304" Width="373"> <StackPanel> <StackPanel.Resources> <Style TargetType="{x:Type Label}"> <Setter Property="FontFamily" Value="Arial" /> <Setter Property="FontSize" Value="12" /> <Setter Property="FontWeight" Value="Bold" /> </Style> </StackPanel.Resources> <Label Label>Nap >Napier University.</Label Label> <Label Label>Is based in Edinburgh</Label Label> </StackPanel> G StoryBoards 21. In XAML it is possible to design a storyboard for animation. Enter the following highlighted code, and prove that when the button is pressed that the opacity goes from 100% to 0% in 5 seconds. <Window x:class="wpfapplication2.window1" Title="Napier" Height="304" Width="373"> <Button Height="40" Width="125"> <Button.Triggers> <EventTrigger RoutedEvent="Button.Click"> <BeginStoryboard> <Storyboard TargetProperty="Opacity"> <DoubleAnimation From="1" To="0" Duration="0:0:05" /> </BeginStoryboard> </Button.Triggers> </Button> W.Buchanan 7

22. Next update the code so that the button increases from 125 pixels to 300 pixels in 5 seconds (as shown next). Modify the code so that the height increases from 40 pixels to 80 pixels. <Storyboard TargetProperty="Width"> <DoubleAnimation From="125" To="300" Duration="0:0:05" /> 23. The following allows for a colour change of a rectangle from Red to Blue, and back again (the source is at http://billdotnet/anim.zip). Run the program, and prove its operation. Change the colour from Black to White, over a period of 5 seconds. <Window x:class="wpfapplication6.window1" Title="Window1" Height="300" Width="300"> <StackPanel Margin="20"> <Rectangle Width="94" Height="94"> <Rectangle.Fill> <SolidColorBrush x:name="animationfill" Color="Red" /> </Rectangle.Fill> <Rectangle.Triggers> <EventTrigger RoutedEvent="Rectangle.MouseEnter"> nter"> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="AnimationFill" Storyboard.TargetProperty="Color" To="Blue" Duration="0:0:2" /> </BeginStoryboard> <EventTrigger RoutedEvent="Rectangle.MouseLeave"> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="AnimationFill" Storyboard.TargetProperty="Color" To="Red" Duration="0:0:2" /> </BeginStoryboard> </Rectangle.Triggers> </Rectangle> </StackPanel> 24. Other events can be added, such as for the left hand mouse button click. Add the following code to the previous example (Ex 23), and prove its operation. Remeber to add it before the end Rectangle.Triggers tags. W.Buchanan 8

<EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="AnimationFill AnimationFill" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:0.5" AutoReverse="True" /> </BeginStoryboard> 25. The following XAML code allows a red rectangle to be moved across the window (http://billdotnet/rect.zip). Check the program, and make sure that it left click mouse event on the rectangle causes it to animate, and a left click at the end will move it back to the start. <Window x:class="wpfapplication6.window1" Title="Window1" Height="300" Width="300"> <Rectangle Name="MovingRect" Fill="Red" Width="50" HorizontalAlignment="Left" Margin="6,100,0,112"> <Rectangle.Style> <Style TargetType="{x:Type Rectangle}"> <Style.Triggers> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)" ransform.(translatetransform.x)" From="0" To="200" /> </BeginStoryboard> </EventTrigger.Actions> </Style.Triggers> </Style> </Rectangle.Style> <Rectangle.RenderTransform> <TranslateTransform X="0" Y="0" /> </Rectangle.RenderTransform> </Rectangle> W.Buchanan 9

26. XAML can contain key frames, where there are breakpoints in the animation. The following move a rectangle from 0 to 350 (for seven seconds), 350 to 50 (for five seconds) and 50 to 200 (in seconds seconds). Check the operation (http://billdotnet/rect2.zip), and then modify to that the same movement occurs in the x-direction (instead of the x-direction). <Window x:class="wpfapplication6.window1" Title="Window1" Height="300" Width="500"> <Rectangle Name="MovingRect" Fill="Red" Width="50" HorizontalAlignment="Left" Margin="6,100,0,112"> <Rectangle.Triggers> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Trans" Storyboard.TargetProperty="X" Duration="0:0:15"> <LinearDoubleKeyFrame Value="350" KeyTime="0:0:7" /> <LinearDoubleKeyFrame Value="50" KeyTime="0:0:5" /> <LinearDoubleKeyFrame Value="200" KeyTime="0:0:3" /> </DoubleAnimationUsingKeyFrames> </BeginStoryboard> </EventTrigger.Actions> </Rectangle.Triggers> <Rectangle.RenderTransform> <TranslateTransform x:name="trans" X="0" Y="0" /> </Rectangle.RenderTransform> </Rectangle> G Using Microsoft Expression Blend 27. If you have Microsoft Expression Blend, create a new project. Next add a red rectangle on the screen, and then add a new timeline from the + button on the Object and Timeline (Figure 5). From the record line move on the 0 second mark, move the rectangle into the centre of the design area, then move to one second. Press the + button on the timeline (Record Keyframe). Then move to 1 second on the timeline, and move the rectangle to the left top-hand side, and press Record Keyframe. Finally move to the 2 second mark, and move the retangle, and press Record Keyframe. Finally go back to the 0 second keyframe, and change the fill colour to White. There should now be a blue dotted line for the path of the rectangle. W.Buchanan 10

Figure 5: Adding a timeline in Blend Figure 6: Showing timeline 28. View the XAML (otherwise load it from http://billdotnet.com/rect3.zip), and integrate it with Visual Studio, and prove its operation. <Window x:class="untitledproject2.window1" x:name="window" Title="Window1" Width="640" Height="480"> <Window.Resources> <Storyboard x:key="timeline1"> W.Buchanan 11

<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="00:00:02" Value="#FFD31818"/> <SplineColorKeyFrame KeyTime="00:00:04" Value="#FFD31818"/> </ColorAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransfor m.x)"> <SplineDoubleKeyFrame KeyTime="00:00:00" Value="218"/> <SplineDoubleKeyFrame KeyTime="00:00:02" Value="0"/> <SplineDoubleKeyFrame KeyTime="00:00:04" Value="367"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransfor m.y)"> <SplineDoubleKeyFrame KeyTime="00:00:00" Value="91"/> <SplineDoubleKeyFrame KeyTime="00:00:02" Value="0"/> <SplineDoubleKeyFrame KeyTime="00:00:04" Value="-4"/> </DoubleAnimationUsingKeyFrames> </Window.Resources> <Window.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <BeginStoryboard Storyboard="{StaticResource Timeline1}"/> </Window.Triggers> <Grid x:name="layoutroot"> <Rectangle RenderTransformOrigin="0.5,0.5" Fill="#FFFFFFFF" Stroke="#FF000000" HorizontalAlignment="Left" Margin="51,45,0,0" x:name="rectangle" VerticalAlignment="Top" Width="186" Height="141"> <Rectangle.RenderTransform> <TransformGroup> <ScaleTransform ScaleX="1" ScaleY="1"/> <SkewTransform AngleX="0" AngleY="0"/> <RotateTransform Angle="0"/> <TranslateTransform X="0" Y="0"/> </TransformGroup> </Rectangle.RenderTransform> </Rectangle> 29. Finally, using Microsoft Expression Blend, draw the Union Jack Flag, and animate it so that it starts at the top-left hand side, moves to the top-right hand, then moves to the bottom right-hand side, and finally moves back to the original place. The flag should fade out when it reaches the top right-hand side. W.Buchanan 12