开发者

seeing C# windows forms project code from F#

开发者 https://www.devze.com 2022-12-25 23:16 出处:网络
I have a C# Windows Forms project open with some C# code in it. Question: How can I have an F# file that I can write F# code in but still referencing all the C# code I have on Form1.cs (including the

I have a C# Windows Forms project open with some C# code in it.

Question: How can I have an F# file that I can write F# code in but still referencing all the C# code I have on Form1.cs (including the GUI).

I can successfully do this:

- Create a C# Windows Forms project

- Create a F# Library project

- Reference the F# Library DLL from my C# project

- That way I can ca开发者_Python百科ll F# functions from C#

But I still can't see my buttons and textboxes from F#

I understand that that is because it's a library and it can't reference System.Windows.Forms

So how do I fix this? I don't want it to be a library or this or that, I just want it to be a file that will allow me to write F# code while being able to reference my C# Form and code.

I guess you can say I want an F# file that is also a "partial class Form1" so that I can continue writing code for the same Project, but using F# instead. How do I do that?


If I understand the question correctly, you're actually asking how to structure your application so that you can use WinForms designer (which is only available in C#/VB) to create a form and at the same time, write the application logic in F#.

First of all, there is no way to mix the two languages in a single project, which means that you cannot write something like a partial class with one part in F# and other part in C# (this is technically almost impossible). So, the only option is to write an F# project and a C# project and reference one from the other. There are two ways to do this:

(Based on your answer and comments, I think you may prefer the first one)

Referencing C# library from F# application: One option is to create a simple WinForms library project in C# to contain the form (and possibly other controls etc.) created using the WinForms designer and nothing else. Then you'd reference this library from an application created in F# and implement all the user interaction in the F# application.

If you make sure to mark the YourForm class created by WinForms designer as well as all relevant controls added to the form as public, then you can reference the form and control it from your F# application.

  • I used this approach in the one sample in my book, so if you're looking for an example, you can look at the source code for Chapter 14 (look for "FSharpEffects")

Referencing F# library from C# application: When you structure your application in this way, you'll need to create an F# library that exposes all the basic functionality that your application needs and then reference it from a C# application. The C# application would add all the user-interface interaction code and it would call the F# library to preform the actual work.

In this scenario, the F# library doesn't know anything about the C# form. If you reference System.Windows.Forms from your F# library then it may take some WinForms controls as arguments (e.g. to configure them & load data), but it won't know anything about the controls declared in C#.

One possible extension to this approach is to declare an interface in the F# library, implement it in the C# application and then pass an implementation back to the F# library. The interface may expose some important properties of the application (e.g. give access to all the important WinForms controls). Once you give an implementation of the interface to your F# library, the F# library can use it to manipulate with the application.

(The approach with interface requires writing more code and may be less flexible, but if you want to write the main application in C# and declare the form there, it is probably the best option)


You need to add a reference to System.Windows.Forms.dll to the F# project, and set the visibility of the Form1 class and its members to public.

(Libraries can also reference System.Windows.Forms; that's what reusable control projects are)

You'll also need some way to call the F# code in the first place; you might want to make the F# project an EXE (that calls Application.Run) and the C# project into a library.

0

精彩评论

暂无评论...
验证码 换一张
取 消