Note, while the content below is valid it is now (7/2018) considered to be a no longer supported feature as the state of
.NET 4.5+ makes it very difficult to continue supporting compiling XAML with MSBuild.
It is recommended when running CS-Script scripts for WPF to load XAML dynamically. This way there is no
need for a custom compiler (MSBuild). The sample that demonstrates the technique can be found here:
Windows Presentation Foundation
) is a framework (first introduced in .NET3.0) for building rich dynamic GUI.
Any C# script utilizing the WPF classes can be executed by CS-Script as any other scripts. It is possible because
from the CLR point of view WPF classes are just ordinary CLR classes.
That is it WPF is nothing else but a collection of CLR classes.
WPF is directly related to XAML - another part of .NET3.0+.
Extensible Application Markup Language
) is a language for describing GUI layout and it's relationships
with the business objects. XAML primarily used in conjunction with WPF. Usually it is used in a compiled form for initialization
GUI layout at runtime and as such it does require it's own compiler.
It is important to note that XAML has nothing to do with CLR. This is the reason why scripts which are utilizing
XAML has to be handled by the script engine in a special way.
WPF script which are not using XAML do not require any special treatment and can be executed normally.
Starting from version 1.9.0 CS-Script includes special edition of the C# compiler capable of processing XMAL files.
The following code sample demonstrates how WPF script can use XAML:
class MainWindow : System.Windows.Window
object sender, RoutedEventArgs e)
MainWindow wnd =
"Hello World Application" Height=
"OnClick" >Say Hello</Button>
The code above is a WPF multi-file "Hello World" script (
Hello.cs + Hello.xaml
) from CS-Script samples library (Script Library). Xaml file can be included in the primary
script as any other secondary script file: with directive
(in short form
Unfortunately MS did not provide XAML compiler as a stand-alone application. Instead MS has merged XAML compiler
and in this form it is used by CS-Script custom compiler
Such approach is inconsistent with the way how C# code is usually processed by CS-Script. And that is why implementation
of the XAML support may change in future depending on MS decision to ship stand-alone version of XAML compiler.
You may also notice that WPF scripts and executables produces from the scripts sometimes demonstrates slight startup-delay.
This is a .NET bag, which was already reported to MS (https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=273495&SiteID=212).
Silverlight is a subset of WPF and as such does not require any special technique to be executed as a script: you just need
to reference appropriate assemblies and include code-behind and XAML files (this can change in the future).
However CS-Script offers an interesting technique for viewing Silverlight content without a browser: Silverlight
Player script (<cs-script>\Lib\silverlight.cs).
Execute the following command in command to playback XAP file (e.g.
cscs silverlight myApp.xap
Silverlight Player is a viewer for XAP (compressed Silverlight content) files. Silverlight Player is capable not only
rendering Silverlight content but also generating self-sufficient executable from XAP file.
Thus you can convert XAP file into WinForm executable which can be executed on any Windows PC having Silverlight
runtime installed. It does not require any other files to be distributed or even temporary created on target PC at
runtime as it hosts it's own built-in Web server, IE Web browser and contains XAP file as embedded resource.
Execute the following command in command prompt to convert XAP file into self-sufficient executable (e.g.
cscs silverlight /exe myApp.xap
To allow using Silverlight Player by right clicking XAP files in Windows
Explorer the "View" and "Convert to EXE" shell extensions have been made available. They can be installed by executing
corresponding batch files from <cs-script>\Samples\Silverlight directory.
Silverlight Player script was inspired by Tamir Khason effort described here:
Silverlight content sample (BookPages.xap) is taken from
SOTC: programming and development tutorial blog