Archive for the ‘Windows Presentation Foundation (WPF)’ Category

Windows Presentation Foundation (WPF) Command-Line Compile Using MSBuild

August 16, 2012

Follow the steps below to build a WPF application using MSBuild.

App.xaml

<Application x:Class=”WpfApplicationHelloWorld.App”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
StartupUri=”MainWindow.xaml”>
<Application.Resources>

</Application.Resources>
</Application>

App.xaml.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace WpfApplicationHelloWorld
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
    }
}

MainWindow.xaml

<Window x:Class="WpfApplicationHelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Background="IndianRed">
        <Label Height="28" Margin="77,28,81,0" Name="lblHello" 
               VerticalAlignment="Top" Opacity="0.5" 
               Background="SlateGray" Foreground="White">
               Label&lt/Label>
        <Button Height="23" Margin="77,66,126,0" Name="btnHello" 
                VerticalAlignment="Top" Click="btnHello_Click" 
                Opacity="0.5">Say Hello&lt/Button>
    &lt/Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfApplicationHelloWorld
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
		private void btnHello_Click(object sender, RoutedEventArgs e)
		{
			lblHello.Content = "Hello WPF!";
		}
    }
}

WpfApplicationHelloWorld.csproj

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<PropertyGroup>
	<AssemblyName>WPFApplicationHelloWorld</AssemblyName>
	<OutputType>winexe</OutputType>
</PropertyGroup>

<ItemGroup>
	<Reference Include="System" />
	<Reference Include="WindowsBase" />
	<Reference Include="PresentationCore" />
	<Reference Include="PresentationFramework" />
	<Reference Include="System.Data" />
	<Reference Include="System.Linq" />
</ItemGroup>

<ItemGroup>
	<ApplicationDefinition Include="App.xaml" />
	<Compile Include="App.xaml.cs" />
	<Page Include="MainWindow.xaml" />
	<Compile Include="MainWindow.xaml.cs" />
</ItemGroup>

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />

</Project> 

Issue the MSBuild command, at the command-line.

Windows Presentation Foundation (WPF) Command-Line Compile Using CSC

August 16, 2012

If you choose to use csc, the C# compiler, then you may only compile C# files, extensible application markup language (XAML) files are none inclusive.

MyWpfApplication.cs

using System;
using System.Windows;
using System.Windows.Controls;

  class MyWpfApplication : Application
  {
    [STAThread]
    static void Main()
    {
      MyWpfApplication app = new MyWpfApplication();
      app.Startup += AppStartUp;
      app.Exit += AppExit;
      app.Run();
    }

    static void AppExit(object sender, ExitEventArgs e)
    {
      MessageBox.Show("Application exiting.");
    }

    static void AppStartUp(object sender, StartupEventArgs e)
    {
      MainWindow wnd = new MainWindow("WPF Application.", 300, 300);
    }
  }

  class MainWindow : Window
  {
    Button btnExitApp = new Button();

    public MainWindow(string windowTitle, int height, int width)
    {
      btnExitApp.Click += new RoutedEventHandler(btnExitApp_Clicked);
      btnExitApp.Content = "Exit Application";
      btnExitApp.Height = 25;
      btnExitApp.Width = 100;
      this.AddChild(btnExitApp);

      this.Title = windowTitle;
      this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
      this.Height = height;
      this.Width = width;
      this.Show();
    }

    private void btnExitApp_Clicked(object sender, RoutedEventArgs e)
    {   
      Application.Current.Shutdown();
    }
  }

MyWpfApplicationBuild.bat

csc.exe /out:MyWpfApplication.exe /target:winexe MyWpfApplication.cs 

/reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\presentationframework.dll" 
/reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\windowsbase.dll" 
/reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\presentationcore.dll"