c# events and wpf #w5. horizontal prototype wpf designed for rapid user interface design used for...
TRANSCRIPT
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/
Extensible Application Markup Language (XAML)
• Separating front-end from back-end• Design space• Tree-like structure • Containers; Canvas, Grid, StackPanel• UIElements; Image, Button, Rect
Some containers
Grid Canvas
StackPanel
Exercise
• Create a WPF project• Create a button in XAML• Now create a button in C#
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.
Events
–
–
–
•–
–
•
Events
Exercise
• Add Mouse is Up event to the button you just created
• Change the content to “clicked”
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"/>
Exercise
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 "Hello"“
click=“Button_Click”/></StackPanel>
<TextBlock x:Name="greetingOutput"/></StackPanel>
…
private void Button_Click(object sender, RoutedEventArgs e) { greetingOutput.Text = "Hello, " + nameInput.Text + "!"; }
Timer
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();}
XAML <Grid x:Name="Grid1">
<Label Name="lblTime" FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid>