Create an F# console application.
Change the Output type of the application to Windows Application.
Add the FsXaml NuGet package.
Add these four source files, in the order listed here.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="First Demo" Height="200" Width="300"> <Canvas> <Button Name="btnTest" Content="Test" Canvas.Left="10" Canvas.Top="10" Height="28" Width="72"/> </Canvas> </Window>
namespace FirstDemo type MainWindowXaml = FsXaml.XAML<"MainWindow.xaml"> type MainWindow() as this = inherit MainWindowXaml() let whenLoaded _ = () let whenClosing _ = () let whenClosed _ = () let btnTestClick _ = this.Title <- "Yup, it works!" do this.Loaded.Add whenLoaded this.Closing.Add whenClosing this.Closed.Add whenClosed this.btnTest.Click.Add btnTestClick
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> </Application.Resources> </Application>
namespace FirstDemo open System type App = FsXaml.XAML<"App.xaml"> module Main = [<STAThread; EntryPoint>] let main _ = let app = App() let mainWindow = new MainWindow() app.Run(mainWindow) // Returns application's exit code.
Delete the Program.fs file from the project.
Change the Build Action to Resource for the two xaml files.
Add a reference to the .NET assembly UIAutomationTypes.
Compile and run.
You can't use the designer to add event handlers, but that's not a problem at all. Simply add them manually in the code behind, like you see with the three handlers in this example, including the handler for the test button.
UPDATE: An alternative and probably more elegant way to add event handlers has been added to FsXaml. You can add the event handler in XAML, same as in C# but you have to do it manually, and then override the corresponding member that turns up in your F# type. I recommend this.