The recommended way to build any WPF
application, however, is to use the code file
approach. Under this model, the XAML files of your project contain
nothing but the markup that describes the general state of your classes,
while the code file contains the implementation details.
1. Adding a Code File for the MainWindow Class
To illustrate, you will
update the WpfAppAllXaml example to use code files. If you are following
along, copy this entire folder and give it the name WpfAppCodeFiles.
Now, create a new C# code file in this folder named MainWindow.xaml.cs (by convention, the name of a C# code-behind file takes the form *.xaml.cs). Add the following code to this new file:
// MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
namespace SimpleXamlApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
// Remember! This method is defined
// within the generated MainWindow.g.cs file.
InitializeComponent();
}
private void btnExitApp_Clicked(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
Here, you have defined a
partial class to contain the event handling logic that will be merged
with the partial class definition of the same type in the *.g.cs file. Given that InitializeComponent() is defined within the MainWindow.g.cs file, your window's constructor makes a call in order to load and process the embedded BAML resource.
The MainWindow.xaml file will also need to be updated; this simply involves gutting all traces of the previous C# code:
<Window x:Class="SimpleXamlApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="A Window built using Code Files!"
Height="200" Width="300"
WindowStartupLocation ="CenterScreen">
<!--The event handler is now in your code file -->
<Button x:Name="btnExitApp" Width="133" Height="24"
Content = "Close Window" Click ="btnExitApp_Clicked"/>
</Window>
2. Adding a Code File for the MyApp Class
If desired, you could also build a code-behind file for your Application-derived type. Because most of the action takes place in the MyApp.g.cs file, the code within MyApp.xaml.cs is little more than the following:
// MyApp.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
namespace SimpleXamlApp
{
public partial class MyApp : Application
{
private void AppExit(object sender, ExitEventArgs e)
{
MessageBox.Show("App has exited");
}
}
}
The MyApp.xaml file now looks like so:
<Application x:Class="SimpleXamlApp.MyApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Exit ="AppExit">
</Application>
3. Processing the Code Files with msbuild.exe
Before you recompile your files using msbuild.exe, you need to update our *.csproj file to account for the new C# files to include in the compilation process, via the <Compile> elements (shown in bold):
<Project DefaultTargets="Build" xmlns=
"http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<RootNamespace>SimpleXamlApp</RootNamespace>
<AssemblyName>SimpleXamlApp</AssemblyName>
<OutputType>winexe</OutputType>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="MyApp.xaml" />
<Compile Include = "MainWindow.xaml.cs" />
<Compile Include = "MyApp.xaml.cs" />
<Page Include="MainWindow.xaml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
</Project>
Once you pass our build script into msbuild.exe,
you find once again the same executable assembly as the WpfAppAllXaml
application. However, as far as development is concerned, you now have a
clean partition of presentation (XAML) from programming logic (C#).
Given
that this is the preferred method for WPF development, you'll be happy
to know that WPF applications created using Visual Studio 2010 always
make use of the code-behind model just presented.