static resources
The static resource of fyne is actually to compile the resource file into a binary file, and then directly pass it in as a variable in the code. The advantage of this is that there is no need for additional image resources when packaging fyne applications .
The following commands are required
//这个命令是把 image1.png 编译为名叫 bundled.go 的二进制文件
fyne bundle image1.png >> bundled.go
//这个命令是把 image1.png 添加到为名叫 bundled.go 的二进制文件
fyne bundle -append image2.png >> bundled.go
The second command specifies -append
the option to add to the existing file, and the generated file is as follows:
// auto-generated
// Code generated by '$ fyne bundle'. DO NOT EDIT.
package main
import "fyne.io/fyne/v2"
// resourceIconIco 就是你要调用的资源变量名
var resourceIconIco = &fyne.StaticResource{
StaticName: "icon.ico",
//下面括号中内容过多,就不放源码了
StaticContent: []byte(...),
}
Then you can call this variable directly in the code. Here is an example of setting the application icon
package main
import (
"fyne.io/fyne/v2/app")
func main() {
a := app.New()
a.SetIcon(resourceIconIco)
a.Run()
}