看到C#窗口从F#形成项目代码 [英] seeing C# windows forms project code from F#
问题描述
我有一个C#Windows窗体项目与一些C#代码开放在它
I have a C# Windows Forms project open with some C# code in it.
问:我怎样才能有一个F#文件我可以写在F#代码,但仍引用所有的C#代码我有Form1.cs中(包括GUI)。
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).
我可以成功地做到这一点:
- 创建一个C#Windows窗体项目
- 创建一个F#库项目
- 从参考的F#库DLL我C#项目
- 这样我可以从C#调用
F#功能
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 call F# functions from C#
但我仍然无法看到我从F#按钮和文本框
我知道,那是因为它是一个库,它不能参考System.Windows.Forms
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
那么,如何我解决这个问题?我不希望它是一个库或这样或那样的,我只是希望它是一个文件,让我写的F#代码,同时能够引用我的C#窗体和代码。
我想你可以说我想要一个F#文件,这也是部分Form1类,让我能继续为同一项目编写代码,但使用F#代替。
我该怎么办呢?
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?
推荐答案
如果我理解正确的问题,你实际上问如何构建您的应用程序,让您可以使用的WinForms设计师(这是只适用于C#/ VB)来创建一个表单,并在同一时间,写在F#应用程序逻辑。
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#.
首先,就没有办法在一个单一的项目,这意味着你不能写东西像部分
类与F#和其他一部分混合两种语言在C#中的一部分(这是技术上的几乎的是不可能的)。所以,唯一的选择是写一个F#项目和一个C#项目,并引用从另一个。有两种方法可以做到这一点:
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:
的(根据你的回答和评论,我想你可能更喜欢第一个)的
引用C#的F#应用程序库:一种选择是创建一个简单的WinForms库项目在C#中包含的形式(也可能是其他控件等)使用的WinForms设计师,没有别的创建。然后你会在F#创建的应用程序中引用该库,并实现在F#应用程序中的所有用户交互。
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.
如果您务必记被添加到的WinForms设计师以及所有相关控件创建的 YourForm
类形式公共
,那么你可以参考表格并从您的F#应用程序来控制它。
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.
- 我曾经在我的书的一个样品在这种方法中,所以如果你正在寻找一个例子,你可以看看< A HREF =http://code.msdn.microsoft.com/realworldfp>为第14章(寻找FSharpEffects)
从C#应用程序中引用F#库::当你以这种方式构建应用程序,你需要创建一个公开的所有基本功能的F#库,您的应用需求再从C#应用程序引用它。 C#应用程序将添加的所有用户界面交互的代码,它会调用F#库瓶坯的实际工作。
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.
在这种情况下,F#库不知道C#的形式什么。如果从F#库引用的 System.Windows.Forms的的话,就可能采取一些WinForms控件作为参数(如对其进行配置和放大器;负载数据),但它不知道声明的任何控制在C#。
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#.
一个可能延长到这种做法是宣布在F#库的接口,在C#应用程序中实现它,然后通过实施回F#库。接口可能会使应用程序的一些重要特性(例如提供访问所有重要的WinForms控件)。一旦你给了接口的实现你的F#库中,F#库可以使用它与应用程序来操作。
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.
的(带接口的方法需要书写更多的代码,可能是不太灵活,但如果你想要写在C#中的主要应用,并声明形式存在,它可能是最好的选择)的
这篇关于看到C#窗口从F#形成项目代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!