簡體   English   中英

處理2個將C#轉換為F#的編譯器錯誤

[英]Handling 2 compiler errors translating C# to 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!" }; Content = new Label { Text = "Hello World!" };

所以快速解決

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!")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM