1

At the risk of beating a dead horse, I would like to be clear on the initial setup for using Elmish.WPF.

My intent is to have essentially two projects:

  1. FrontOfficeV -- a C# project to hold the XAML's, Windows, etc..
  2. Models -- a F# project for Elmish.WFP, F# code, etc..

After trying most all of the project type options (Visual Studio 2019), I settled on: Console App (.NET Framework) C# for the C# Project.

This created the C# project that defaulted to: Target framework: .NET Framework 4.7.2 Output type: Console Application. This also created a "program.cs" file.

Next, using ADD -->New Item with the C# Console App did not show a "Window" option. It did show the "User Control (WPF)" option. So the "User Control (WPF) was added and renamed to "MainWindow". I then changed the element tag in the xaml from UserControl to Window and changed the code behind to inherit from Window instead of UserControl. Lastly, I changed the project properties to compile output type to: Windows Application. This C# project was now marked as the "StartUp" project for visual studio.

Next, the F# Models project was created using: Library (.NET Framework) F# Windows library. This created a project which defaulted to: Target framework: .NET Framework 4.7.2 Output type: Class Library. To the F# class library, a program -- FrontOffice.fs, was added with:

/// This is the application's entry point. It hands things off to Elmish.WPF     
    let entryPoint (mainWindow: Window) =    
        Program.mkSimpleWpf init update bindings
        |> Program.runWindowWithConfig
                   { ElmConfig.Default with LogTrace = true; Measure = true; MeasureLimitMs = 1 }
                   mainWindow

Lastly, the program.cs was changed to read:

public class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {
            Application app = new Application();
            Window mainWindow = new MainWindow();
            Models.FrontOffice.entryPoint(mainWindow);
        }
    }

The point here being the usage of: Program.mkSimpleWpf.

Why all the hassle? I would like to use the full WPF/C#/XAML goodies (like Themes, custom controls, etc), and I am not so sure the direct compilation of XAML in F# can do this. I'd like to be able to use the F# data types in the XAML, so the C# project requires a reference to the F# Modules, so it seems easier to create the windows in C# and pass a reference unto the F# Modules entryPoint, as above.

Questions:

  1. Is the setup above correct for Elmish.WPF?
  2. Is there a better setup to accomplish my goals?
  3. As of Visual Studio 2019, does F# compilation of the xaml work for themes, custom controls, etc? What hiccups can I expect?

Thank you for any clarification on these points. TIA

Alan Wayne
  • 5,122
  • 10
  • 52
  • 95
  • 1
    Fwiw i think a better setup would be regular mvvm without any trees and just c#. Good luck. – Andy Aug 20 '20 at 10:34
  • 3
    I think the way my [ExploreElmishWpf](https://github.com/BentTranberg/ExploreElmishWpf) application is structured, is the way to go. It uses a C# WPF Application to hold the XAML which is BAMLed, and an F# library to hold all the source, except possibly for some trivial code behind in C# when preferred, which is also just great. Design time models in F#, the Elmish.WPF way, are then easy. I've used this for years in my commercial projects. – Bent Tranberg Aug 20 '20 at 16:57
  • 2
    @alan-wayne I don't understand what you mean by *I am not so sure the direct compilation of XAML in F# can do this*. As I understand it, you are using XAML in the C# project, not the F# project. In any case, I think all the samples in the Elmish.WPF repo demonstrate an approach that works well. The C# XAML project is the entry point, and refers to the F# project with your domain logic, Elmish.WPF bindings, etc. There is even a sample that shows how to pass window constructors to the F# code (e.g. the NewWindow sample). The samples lean into the tooling to ensure a good experience. – cmeeren Aug 21 '20 at 05:59
  • "Why all the hassle?" Do you know way to do it without "all the hassle"? I am always looking for ways to make things simpler, so if you have an idea, then I am eager to listen. – Tyson Williams Aug 23 '20 at 19:02
  • "I'd like to be able to use the F# data types in the XAML" I don't know what you mean by this. Can you elaborate here? Maybe give an example? – Tyson Williams Aug 23 '20 at 19:04

0 Answers0