c# events and wpf #w5. horizontal prototype wpf designed for rapid user interface design used for...

20
C# Events and WPF #W5

Upload: edward-poole

Post on 12-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

C# Events and WPF

#W5

Page 2: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,
Page 3: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Horizontal Prototype

Page 4: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

WPF

• Designed for rapid user interface design • Used for many devices: Windows Phone,

Tablets, PCs,

Page 5: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

• Microsoft Virtual Academy• http://www.microsoftvirtualacademy.com/

• Microsoft Developer Network• https://msdn.microsoft.com/

Page 6: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Extensible Application Markup Language (XAML)

• Separating front-end from back-end• Design space• Tree-like structure • Containers; Canvas, Grid, StackPanel• UIElements; Image, Button, Rect

Page 7: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Some containers

Grid Canvas

StackPanel

Page 8: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Exercise

• Create a WPF project• Create a button in XAML• Now create a button in C#

Page 9: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

C# code…public MainWindow(){

InitializeComponent();

Button button1 = new Button();button1.Content = "Button";button1.Click += Button_Click;

this.Grid1.Children.Add(button1);}

private void Button_Click(object sender, RoutedEventArgs e){

throw new NotImplementedException();}

// and add a Click event hanler.

Page 10: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Events

Page 11: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

•–

Page 12: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Events

Page 13: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Exercise

• Add Mouse is Up event to the button you just created

• Change the content to “clicked”

Page 14: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Solution

private void MouseIsUp(object sender, MouseButtonEventArgs e){

bt.Content = "Clicked";}<Button x:Name="bt" Content="Button" HorizontalAlignment="Left" Margin="254,202,0,0" VerticalAlignment="Top" Width="74" MouseUp="MouseIsUp"/>

Page 15: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Exercise

Page 16: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Solution

<StackPanel Grid.Row="1" Margin="120,30,0,0"><TextBlock Text="What's your name?"/>

<StackPanel Orientation="Horizontal" Margin="0,20,0,20"><TextBox x:Name="nameInput" Width="300"

HorizontalAlignment="Left"/><Button Content="Say &quot;Hello&quot;“

click=“Button_Click”/></StackPanel>

<TextBlock x:Name="greetingOutput"/></StackPanel>

Page 17: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

private void Button_Click(object sender, RoutedEventArgs e) { greetingOutput.Text = "Hello, " + nameInput.Text + "!"; }

Page 18: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

Timer

Page 19: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

C#using System.Windows.Threading;

public void DispatcherTimerSample(){

InitializeComponent();DispatcherTimer timer = new DispatcherTimer();timer.Interval = TimeSpan.FromSeconds(1);timer.Tick += timer_Tick;timer.Start();

}

void timer_Tick(object sender, EventArgs e){

lblTime.Content = DateTime.Now.ToLongTimeString();}

Page 20: C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,

XAML <Grid x:Name="Grid1">

<Label Name="lblTime" FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid>