{"id":3206,"date":"2015-05-28T20:46:54","date_gmt":"2015-05-28T20:46:54","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3206"},"modified":"2015-05-28T20:46:54","modified_gmt":"2015-05-28T20:46:54","slug":"creating-a-wpf-application-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-a-wpf-application-in-f\/","title":{"rendered":"Creating a WPF application in F#"},"content":{"rendered":"<p>I wanted to create a <em>bare bones<\/em> WPF application using F#. There are several templates out there to create WPF apps in F# but they just added too much code from the outset for my liking.<\/p>\n<p>So here are the steps to create a basic WPF F# application<\/p>\n<ul>\n<li>Create a new project and select F# Application<\/li>\n<li>This will be a console application, so next select the project properties and change the Application | Output type from Console Application to Windows Application<\/li>\n<li>Add references to PresentationCore, PresentationFramework, System.Xaml and WindowsBase<\/li>\n<li>Change the Program.fs to look like this\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nopen System\r\nopen System.Windows\r\nopen System.Windows.Controls\r\nopen System.Windows.Markup\r\n\r\n&#x5B;&lt;STAThread&gt;]\r\n&#x5B;&lt;EntryPoint&gt;]\r\nlet main argv = \r\n    let mainWindow = Application.LoadComponent(\r\n                        new System.Uri(&quot;\/&lt;Your Assembly Name&gt;;component\/MainWindow.xaml&quot;, UriKind.Relative)) :?&gt; Window\r\n\r\n    let application = new Application()\r\n    application.Run(mainWindow)\r\n<\/pre>\n<p>Obviously change &lt;Your Assembly Name&gt; to the name of your compiled assembly name.\n<\/li>\n<li>Add a new item to your project. Unfortunately there&#8217;s no XAML file (at least not in the version of Visual Studio I&#8217;m testing this on). So simply pick an XML File and give it the name MainWindow.xaml<\/li>\n<li>In the MainWindow.xaml file replace the existing text with\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Window \r\n    xmlns=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation&quot;\r\n    xmlns:x=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml&quot;\r\n    Title=&quot;MainWindow&quot; Height=&quot;350&quot; Width=&quot;525&quot;&gt;\r\n    &lt;Grid&gt;\r\n    &lt;\/Grid&gt;\r\n&lt;\/Window&gt;\r\n<\/pre>\n<\/li>\n<li>Finally, select the MainWindow.xaml file in the solution explorer and change the file properties Build Action to Resource<\/li>\n<\/ul>\n<p>And that&#8217;s all there is to getting a WPF F# application started. <\/p>\n<p>As F# doesn&#8217;t contain all the goodness of WPF templates and code behind etc. out of the box, it still might be better to create a C# WPF application then have all the view model&#8217;s etc. in a F# application if you want to, but this is a fairly good starting point if you prefer 100% F#.<\/p>\n<p><strong>Addendum<\/strong><\/p>\n<p>After working this stuff out and documenting it, I came across a post from MSDN Magazine titled <a href=\"https:\/\/msdn.microsoft.com\/en-us\/magazine\/hh394149.aspx\" target=\"_blank\">Build MVVM Applications in F#<\/a> which not only shows a more information on the approach I&#8217;ve outlined above, but also how to use F# to create view models etc. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to create a bare bones WPF application using F#. There are several templates out there to create WPF apps in F# but they just added too much code from the outset for my liking. So here are the steps to create a basic WPF F# application Create a new project and select F# [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6,13],"tags":[],"class_list":["post-3206","post","type-post","status-publish","format-standard","hentry","category-f","category-wpf"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3206"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3206\/revisions"}],"predecessor-version":[{"id":3216,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3206\/revisions\/3216"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}