Home Course Index << Prev. Next >> PDF Version of this Page

Course API Comparison
Chapter C4: A WPF Windows Application written in Xaml and C#


Copyright © by V. Miszalok, last update: 21-09-2010


For an introduction into Microsofts XAML see:

XAML

For an introduction into Microsofts WPF-API see:

WPF

Comparison of Microsofts Windows Forms API with WPF:

Compare Windows Forms and WPF

Preliminaries

Guidance for Visual C# 2010 Express Edition:

1) Main Menu after start of VS 2010: Tools → Options → check lower left checkbox: Show all Settings → Projects and Solutions → Visual Studio projects location: → C:\temp.

2) Main Menu after start of VS 2010: File → New Project... → Visual Studio installed templates: Empty Project
Name: compareWPF_Xaml
→ OK.

3) Main Menu after start of VS 2010: Project... → compareWPF_Xaml Properties → Application → Output type: switch from Console Application to Windows Application.

4) In the Solution Explorer-window right click the branch References. Add Reference → .NET-tab scroll down until you find Presentation Core. Click OK. Check whether Presentation Core arrived in the branch References. In the same way you have to add 3 more references: Presentation Framework, System and WindowsBase.

5) In the Solution Explorer-window right click the main branch compareWPF_Xaml. Choose Add → New Item → Text File → Name: compareWPF_Xaml.xaml.

6) In the Solution Explorer-window right click the main branch compareWPF_Xaml. Choose Add → New Item → Code File → Name: compareWPF_Xaml.cs.
 
 

compareWPF_Xaml.xaml

Write the following lines into the empty window of compareWPF_Xaml.xaml:

<Window 
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="myNameSpace.WPF_Xaml"
 Title="WPF Windows Program in XAML + C#"
 Width="500" Height="80" x:Name="window">
 <Viewbox>
  <Border BorderBrush="Black" BorderThickness="2">
   <StackPanel Orientation="Horizontal" Margin="2">
    <Button Content="Talk!" Click="button1Click" HorizontalAlignment="Left"/>
    <TextBox x:Name="textBox" Margin="2,0,2,0" MinWidth="300" TextAlignment="Center"/>
    <Button Content="Clear" Click="button2Click" HorizontalAlignment="Right"/>
   </StackPanel>
  </Border>
 </Viewbox>
</Window>
 

compareWPF_Xaml.cs

Write the following lines into the empty window of compareWPF_Xaml.cs:

using System;
using System.Windows;
using System.Windows.Controls;
namespace myNameSpace
{ public partial class WPF_Xaml : Window
  { [STAThread] public static void Main() 
    { new Application().Run( new WPF_Xaml() );
    }
    public WPF_Xaml() //constructor
    { InitializeComponent();
    }
    private void button1Click( Object sender, EventArgs e )
    { textBox.Text = "WPF Windows Program in XAML + C#. Resize!";
    }
    private void button2Click( Object sender, EventArgs e )
    { textBox.Text = "";
    }
  }
}

Click Debug → Start Without Debugging Ctrl F5.
Click the Talk!-button and resize the window.

top of page: