英文:
How to resize a compose window programmatically after it has been created
问题
Is there a way to resize a Compose Multiplatform desktop window after it has been created?
我已经尝试通过windowState进行设置(这是有道理的,因为它说对初始值的更改不会在创建后更改或重新创建状态),以及通过window.size进行设置。
这两种方法都没有产生任何效果。
fun main() = application {
val windowState = rememberWindowState()
Window(
onCloseRequest = ::exitApplication,
state = windowState
) {
windowState.size = DpSize(1000.dp, 1000.dp) // 不起作用
window.size = Dimension(1000, 1000) // 也不起作用
}
}
英文:
Is there a way to resize a Compose Multiplatform desktop window after it has been created?
I already tried setting it through the windowState (which makes sense that it doesn't work as it says changes to the initial values won't change or recreate the state after it has been created) and through window.size.
Both approaches lead to nothing.
fun main() = application {
val windowState = rememberWindowState()
Window(
onCloseRequest = ::exitApplication,
state = windowState
) {
windowState.size = DpSize(1000.dp, 1000.dp) // not working
window.size = Dimension(1000, 1000) // also not working
}
}
答案1
得分: 2
不应该直接从可组合函数中更改组合状态。而是应该使用副作用 - 详细信息请查看文档。
LaunchedEffect(Unit) {
windowState.size = DpSize(1000.dp, 1000.dp)
}
英文:
You shouldn't change compose state directly from composable functions. Use side effects instead - check out documentation for more details
LaunchedEffect(Unit) {
windowState.size = DpSize(1000.dp, 1000.dp)
}
答案2
得分: -1
我找到了设置大小在我的情况下不起作用的原因。
设置大小的调用太靠近窗口的创建。
如果在调用之前加入1000毫秒的延迟(使用LaunchedEffect或协程范围),两种方法都有效。
英文:
I found the reason why setting the size didn't work in the my case.
The calls to set the size were to close to the creation of the window.
Both methods are working if i put a delay of 1000ms before the calls (either with LaunchedEffect or with a coroutine scope).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论