Posts Tagged ‘csc’

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"