gpt4 book ai didi

swiftui - 如何为 Scene.handlesExternalEvents 发送事件(匹配 :) to receive?

转载 作者:行者123 更新时间:2023-12-04 12:35:42 27 4
gpt4 key购买 nike

在 2020 Apple 平台 API 测试版的中途,添加了主题中提到的方法。 (在 AnyView 中添加了类似的方法。)有谁知道首先发送外部事件的相应 API 在哪里?

最佳答案

使用发送 Scene.handlesExternalEvents 在 macOS 中打开新窗口的示例

@main
struct TestAppApp: App {
var body: some Scene {
WindowGroup {
MainView()
}
//subscribe on event of open mainView
.handlesExternalEvents(matching: Set(arrayLiteral: Wnd.mainView.rawValue))

WindowGroup {
HelperView()
}
//subscribe on event of open helperView
.handlesExternalEvents(matching: Set(arrayLiteral: Wnd.helperView.rawValue))
}
}

enum Wnd: String, CaseIterable {
case mainView = "MainView"
case helperView = "OtherView"

func open(){
if let url = URL(string: "taotao://\(self.rawValue)") {
print("opening \(self.rawValue)")
NSWorkspace.shared.open(url)
}
}
}
和 2 个窗口/ View 代码:
extension TestAppApp {
struct MainView: View {
var body: some View {
VStack {
Button("Open Main View") {
Wnd.mainView.open()
}

Button("Open Other View") {
Wnd.helperView.open()
}
}
.padding(150)
}
}

struct HelperView: View {
var body: some View {
HStack {
Text("This is ") + Text("Helper View!").bold()
}
.padding(150)
}
}
}

Info.plist 也需要更改:
enter image description here

关于swiftui - 如何为 Scene.handlesExternalEvents 发送事件(匹配 :) to receive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65105984/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com