我希望弹出一个模式,在回答模式上的问题之前不会让用户与应用程序交互。通常背景是灰色的,就像我在 C# 表单中所习惯的那样。
我编写了以下代码来尝试 ShowModalPopUp 和 ShowPopUp,两者都会生成弹出窗口。但是 widget.ShowModalPopUp 会生成一个普通的弹出窗口,而不是模态弹出窗口,它保持焦点并使背景变灰。谷歌搜索=“模态弹出表单”看看我期待什么。
package main
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
a := app.New()
w := a.NewWindow("Pop up demo")
w.Resize(fyne.NewSize(600, 600))
//-------------------------------
//func ShowModalPopUp(content fyne.CanvasObject, canvas fyne.Canvas)
//func ShowPopUp(content fyne.CanvasObject, canvas fyne.Canvas)
//https://developer.fyne.io/api/v2.4/widget/
hello := widget.NewLabel("Demo Fyne show popup vs show modal popup")
w.SetContent(container.NewVBox(
hello,
widget.NewButton("pop demo!", func() {
hello.SetText("Show me a popup")
myWindowPop := a.NewWindow("CanvasPop")
myCanvasPop := myWindowPop.Canvas()
red := color.NRGBA{R: 180, G: 0, B: 0, A: 255}
rectPop := canvas.NewRectangle(&red)
rectPop.SetMinSize(fyne.NewSize(200, 200))
myCanvasPop.SetContent(rectPop)
widget.ShowPopUp(rectPop, myCanvasPop) // show popup
myWindowPop.Resize(fyne.NewSize(300, 300))
myWindowPop.Show()
}),
widget.NewButton("Modal demo", func() {
hello.SetText("show me a ModalPopup")
myWindow := a.NewWindow("CanvasModal")
myCanvas := myWindow.Canvas()
blue := color.NRGBA{R: 0, G: 0, B: 180, A: 255}
rect := canvas.NewRectangle(&blue)
rect.SetMinSize(fyne.NewSize(200, 200))
myCanvas.SetContent(rect)
widget.ShowModalPopUp(rect, myCanvas) // show modal popup
myWindow.Resize(fyne.NewSize(300, 300))
myWindow.Show()
}),
))
w.ShowAndRun()
}
模态弹出窗口应该按照您的描述工作。 您可能对提供标准模式对话框的对话框包感兴趣。 不管怎样,如果模态显示不起作用,那么对话框也不起作用?
我怀疑问题在于您正在为模式创建新窗口。但要成为模式,它必须与您希望它覆盖的内容位于同一窗口上。
经过一番努力终于我的模态弹出窗口可以工作了,也感谢 Andy.xyz。 我希望 fyne 文档中有更多的小代码示例。 我在这里发布我的工作解决方案,供任何人评论和/或进行改进。谢谢。
代码有一个主窗口和按钮,可打开一个带有关闭模态按钮的模态。
package main
import (
"image/color"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
// Create a new Fyne application
a := app.New()
// Create a new window with a title
w := a.NewWindow("Pop up demo by DaHerb")
// Create a label widget with a message
hello := widget.NewLabel("Demo Fyne show popup vs show modal popup")
// Create a button widget to trigger the pop-up
showPopUpButton := widget.NewButton("Show Pop-up", nil)
// Define content and canvas objects for the pop-up
content := canvas.NewText("This is the content of the pop-up",
color.Black)
// Declare a variable to hold the pop-up widget
var popup *widget.PopUp
// Define an action when the "Show Pop-up" button is tapped
showPopUpButton.OnTapped = func() {
// Create a container for the pop-up content, including a "Close"
button
popUpContent := container.NewVBox(
content,
widget.NewButton("Close", func() {
popup.Hide() // Function to hide the pop-up
}),
)
// Create a modal pop-up using the content and the main window's
canvas
popup = widget.NewModalPopUp(popUpContent, w.Canvas())
// Show the modal pop-up
popup.Show()
}
// Set the main window's content to include the label and the Show-Pop-
up button
w.SetContent(container.NewVBox(
hello,
showPopUpButton,
))
// Show the main window and run the application
w.ShowAndRun()
}