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

哈哈 阅读:123 2025-06-02 22:19:02 评论:0

我在将此代码移植到 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!"};

请您参考如下方法:

快速修复

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


标签:C#
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号