Genie 中 vala lambda 的替代品

Alternatives to vala lambdas in Genie

Genie 中缺少 lambda 给工作流带来了一些问题。有一个特殊情况我无法规避。

我在这个特定练习中的目标是创建一个笔记本,其中有一个按钮,单击该按钮会将用户带到同一笔记本的下一页。有四页,最后一页上的按钮会将用户带回第一页(类似于 )。

似乎在 vala 中,使用 lambda 可以轻松完成。我尝试了建议 使用 class 中共享的变量的方法,但问题是尽管我设法访问了调用函数中的变量(回调?仍然不完全确定特定的行话) button.click.connect,仍然无法识别为 Notebook。

这是我的方法:

[indent=4]
uses Gtk

class TestWindow : Window
    notebook:Gtk.Notebook

    init
        // General characteristics of the window
        title = "Gtk Containers"
        default_height = 250
        default_width = 250
        window_position = WindowPosition.CENTER
        destroy.connect(Gtk.main_quit)

        // Now building the notebook
        var notebook = new Gtk.Notebook()
        var label1 = new Gtk.Label("Page one")
        var label2 = new Gtk.Label("Page two")
        var label3 = new Gtk.Label("Page three")
        var label4 = new Gtk.Label("Page four")

        var child1 = new Button.with_label ("Go to next page")
        child1.clicked += def ()
            notebook.set_current_page(2)
        var child2 = new Button.with_label ("Go to next page")
        child2.clicked += def ()
            notebook.set_current_page(3)
        var child3 = new Button.with_label ("Go to next page")
        child3.clicked += def ()
            notebook.set_current_page(4)
        var child4 = new Button.with_label ("Go to first page")
        child4.clicked += def ()
            notebook.set_current_page(1)

        notebook.append_page(child1, label1)
        notebook.append_page(child2, label2)
        notebook.append_page(child3, label3)
        notebook.append_page(child4, label4)

        // Now building the grid
        var grid = new Grid()
        var button1 = new Gtk.Button.with_mnemonic("Button_1")
        var button2 = new Button.with_mnemonic("Button 2")

        // Attaching all elements into the grid
        grid.attach(notebook, 0,0,2,1)
        grid.attach(button1, 0,1,1,1)
        grid.attach(button2, 1,1,1,1)
        add(grid)


init
    Gtk.init (ref args)
    var test = new TestWindow ()
    test.show_all ()
    Gtk.main ()

我在运行时间得到的错误:

(gtkcontainerswithgrid:23039): Gtk-CRITICAL **: gtk_notebook_append_page: assertion 'GTK_IS_NOTEBOOK (notebook)' failed

所以我想 notebook.set_current_page(2) notebook 没有继承 Notebook 的属性。

我希望得到一些关于如何规避这个问题的建议,因为我 运行 没有想法。我尝试创建函数来替代已弃用的语法 += def(),但我无意中遇到了类似的问题。

uses Gtk
class TestWindow : Window
    notebook:Gtk.Notebook
    init
        // General characteristics of the window
        title = "Gtk Containers"
        default_height = 250
        default_width = 250
        window_position = WindowPosition.CENTER
        destroy.connect(Gtk.main_quit)

        // Now building the notebook
        notebook = new Gtk.Notebook()
        var label1 = new Gtk.Label("Page one")
        var label2 = new Gtk.Label("Page two")
        var label3 = new Gtk.Label("Page three")
        var label4 = new Gtk.Label("Page four")

        var child1 = new Button.with_label ("Go to next page")
        child1.clicked.connect (childclicked1)
        var child2 = new Button.with_label ("Go to next page")
        child2.clicked.connect (childclicked2)
        var child3 = new Button.with_label ("Go to next page")
        child3.clicked.connect (childclicked3)
        var child4 = new Button.with_label ("Go to first page")
        child4.clicked.connect (childclicked4)


        notebook.append_page(child1, label1)
        notebook.append_page(child2, label2)
        notebook.append_page(child3, label3)
        notebook.append_page(child4, label4)

        // Now building the grid
        var grid = new Grid()
        var button1 = new Gtk.Button.with_mnemonic("Button_1")
        var button2 = new Button.with_mnemonic("Button 2")

        // Attaching all elements into the grid
        grid.attach(notebook, 0,0,2,1)
        grid.attach(button1, 0,1,1,1)
        grid.attach(button2, 1,1,1,1)
        add(grid)

    def childclicked1()
        notebook.set_current_page(1)

    def childclicked2()
        notebook.set_current_page(2)

    def childclicked3()
        notebook.set_current_page(3)

    def childclicked4()
        notebook.set_current_page(0)

init
    Gtk.init (ref args)
    var test = new TestWindow ()
    test.show_all ()
    Gtk.main ()

我认为唯一的选择就是这个。不支持。