How to use multi bindings in v2? #1499
-
As the document says: In v1, the runtime was available via a struct passed to WailsInit(). In v2, the runtime has been moved out to its own package. Each method in the runtime takes the context.Context that is passed to the OnStartup method. I create another binding in v1,add many bindings with runtime are easy because there is no ctx needed. Now i wonder how to do in v2. main.go//go:embed frontend/dist
var assets embed.FS
func main() {
app := NewApp()
conf := NewConf()// the new binding
err := wails.Run(&options.App{
Title: "genshin-wails",
Width: 1024,
Height: 768,
Assets: assets,
OnStartup: app.startup,
Bind: []interface{}{
app, conf,
},
})
if err != nil {
println("Error:", err)
}
} app.gotype App struct {
ctx context.Context
}
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
} conf.gotype Config struct {
ctx context.Context
}
func NewConf() *Config {
return &Config{}
}
//the method below can not run normally
func (c *Config) SetOutDir() string {
c.ctx = context.Background()// this context is wrong, it should be the same with app's ctx
runtime.LogInfo(c.ctx, "log info")
runtime.BrowserOpenURL(c.ctx, "e:/")
return "success"
} The method |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Good question! There are many ways (as this is just a Go question). For this scenario I would use a function in
|
Beta Was this translation helpful? Give feedback.
Good question! There are many ways (as this is just a Go question). For this scenario I would use a function in
OnStartup
to call methods on your structs with the context.