处理2个编译器错误,将C#转换为F#

时间:2014-01-26 03:14:25

标签: c# f#

我在将此代码移植到f#

时遇到了一些麻烦
public class MyForm : Form
{
    public MyForm ()
    {
        Text = "My Cross-Platform App";
        Size = new Size (200, 200);
        Content = new Label { Text = "Hello World!" };
    }

    [STAThread]
    static void Main () {
        var app = new Application();
        app.Initialized += delegate {
            app.MainForm = new MyForm ();
            app.MainForm.Show ();
        };
        app.Run ();
    }
}

open System
open Eto.Forms
open Eto.Drawing

type MyWindow()=
    inherit Form()
    override this.Size = Size(500,500)
    override this.Text = "test" // no abstract property was found
    override this.Content = new Label() // no abstract property was found

[<STAThread>]
[<EntryPoint>]
let main argv = 
    let app = new Application()
    app.Initialized.Add( fun e -> app.MainForm <- new Form()
                                  app.MainForm.Show())
    app.Run()
    0 // return an integer exit code

我有一些问题:

1.。)如何从基类访问成员?

{
    Text = "My Cross-Platform App";
    Size = new Size (200, 200);
    Content = new Label { Text = "Hello World!" };
}

我尝试使用覆盖,但它只适用于大小而不适用于内容和文本。

2.。)如何将此行翻译为f#Content = new Label { Text = "Hello World!" };

1 个答案:

答案 0 :(得分:6)

快速修复

type MyWindow()=
    inherit Form()
    override this.Size = Size(500,500)
    override this.Text = "test" // no abstract property was found
    override this.Content = new Label() // no abstract property was found

应该是

type MyWindow() as this =
    inherit Form()
    do this.Size <- Size(500,500)
    do this.Text <- "test" 
    do this.Content <- new Label() 

最后,

Content = new Label { Text = "Hello World!" }

let Content = new Label(Text = "Hello World!")