如何使用 fyne 避免 GUI 应用程序中的循环依赖?

我想将 GUI 添加到我用 Go 编写的命令行应用程序,但我遇到了 fyne 和循环依赖性问题。


考虑这个简单的例子来说明我面临的问题:假设一个按钮在我的模型类上触发了一个耗时的方法(比如获取数据等)并且我希望视图在任务完成时更新。


我首先实现了一个非常幼稚且完全不解耦的解决方案,这显然会遇到 go 编译器引发的循环依赖错误。考虑以下代码:


主程序


package main


import (

    "my-gui/gui"

)


func main() {

    gui.Init()

}

gui/gui.go


package gui


import (

    "my-gui/model"

    //[...] fyne imports

)


var counterLabel *widget.Label


func Init() {

    myApp := app.New()

    myWindow := myApp.NewWindow("Test")


    counterLabel = widget.NewLabel("0")


    counterButton := widget.NewButton("Increment", func() {

        go model.DoTimeConsumingStuff()

    })


    content := container.NewVBox(counterLabel, counterButton)


    myWindow.SetContent(content)

    myWindow.ShowAndRun()

}


func UpdateCounterLabel(value int) {

    if counterLabel != nil {

        counterLabel.SetText(strconv.Itoa(value))

    }

}

模型/模型.go


package model


import (

    "my-gui/gui" // <-- this dependency is where it obviously hits the fan

    //[...]

)


var counter = 0


func DoTimeConsumingStuff() {

    time.Sleep(1 * time.Second)

    

    counter++


    fmt.Println("Counter: " + strconv.Itoa(counter))

    gui.UpdateCounterLabel(counter)

}

所以我想知道如何正确解耦这个简单的应用程序以使其正常工作。我在想什么:


使用 fyne 数据绑定:这应该适用于简单的东西,例如上面示例中的标签文本。但是,如果我必须根据模型的状态以非常自定义的方式更新更多内容怎么办?假设我必须根据模型的条件更新按钮的启用状态。这怎么能绑定到数据呢?这可能吗?


使用标准 MVC 设计模式中的接口:我也尝试过这个,但无法真正理解它。我创建了一个单独的模块,它将提供一个接口,然后可以由模型类导入。然后我会注册一个视图(隐式地)实现与模型的接口。但我无法让它工作。我认为此时我对 go 接口的理解还不够。


短轮询模型:这只是meh,当然不是 Go 和/或 fyne 的开发人员的意图:-)


任何人都可以指出这个问题的惯用解决方案吗?我可能在这里遗漏了一些非常非常基本的东西......


肥皂起泡泡
浏览 89回答 1
1回答

蝴蝶不菲

返回值你可以返回值。func DoTimeConsumingStuff() int {&nbsp; &nbsp; time.Sleep(1 * time.Second)&nbsp; &nbsp; counter++&nbsp; &nbsp; return counter}然后在单击按钮时生成一个匿名 goroutine,以免阻塞 UI。counterButton := widget.NewButton("Increment", func() {&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; counter := model.DoTimeConsumingStuff(counterChan)&nbsp; &nbsp; &nbsp; &nbsp; UpdateCounterLabel(counter)&nbsp; &nbsp; }()&nbsp; &nbsp; &nbsp;&nbsp;})打回来您可以将该UpdateCounterLabel函数传递给您的模型函数,也就是回调。func DoTimeConsumingStuff(callback func(int)) {&nbsp; &nbsp; time.Sleep(1 * time.Second)&nbsp; &nbsp; counter++&nbsp; &nbsp; callback(counter)}counterButton := widget.NewButton("Increment", func() {&nbsp; &nbsp; go model.DoTimeConsumingStuff(UpdateCounterLabel)})渠道也许您还可以将一个通道传递给您的模型函数。但是使用上述方法,这似乎不是必需的。潜在地,如果你有不止一个反价值。func DoTimeConsumingStuff(counterChan chan int) {&nbsp; &nbsp; for i := 0; i < 10; i++ {&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(1 * time.Second)&nbsp; &nbsp; &nbsp; &nbsp; counter++&nbsp; &nbsp; &nbsp; &nbsp; counterChan <- counter&nbsp; &nbsp; }&nbsp; &nbsp; close(counterChan)}然后在 GUI 中,您可以从通道接收,再次在 goroutine 中,以免阻塞 UI。counterButton := widget.NewButton("Increment", func() {&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; counterChan := make(chan int)&nbsp; &nbsp; &nbsp; &nbsp; go model.DoTimeConsumingStuff(counterChan)&nbsp; &nbsp; &nbsp; &nbsp; for counter := range counterChan {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UpdateCounterLabel(counter)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()&nbsp; &nbsp; &nbsp;&nbsp;})当然,您也可以再次使用在每次迭代时调用的回调。
打开App,查看更多内容
随时随地看视频慕课网APP