Swiftui 如何使用@FocusedBinding

Swiftui 如何使用@FocusedBinding,swiftui,Swiftui,我尝试使用新的属性包装器@FocusedBinding,但没有成功 下面给出的代码示例是在iOS 14和Big Sur的beta 1测试期间编译的,但对于启用键盘快捷键的操作系统来说,它似乎并不适用于这两种操作系统 有没有人知道在这段时间内是否有什么东西发生了变化,以及如何发生变化,或者什么东西仍在开发中 // This example runs on macOS, iOS, and iPadOS. // // Big Sur Seed 1 has some known issues that

我尝试使用新的属性包装器
@FocusedBinding
,但没有成功

下面给出的代码示例是在iOS 14和Big Sur的beta 1测试期间编译的,但对于启用键盘快捷键的操作系统来说,它似乎并不适用于这两种操作系统

有没有人知道在这段时间内是否有什么东西发生了变化,以及如何发生变化,或者什么东西仍在开发中

// This example runs on macOS, iOS, and iPadOS.
//
// Big Sur Seed 1 has some known issues that prevent state-sharing between 
// windows and the main menu, so this example doesn't currently behave as 
// expected on macOS. Additionally, the Commands API is disabled on iOS in Seed 
// 1. These issues will be addressed in future seeds.
//
// The Focused Value API is available on all platforms. The Commands and
// Keyboard Shortcut APIs are available on macOS, iOS, iPadOS, and
// tvOS—everywhere keyboard input is accepted.
@main
struct MessageApp : App {
        var body: some Scene {
                WindowGroup {
                        MessageView()
                }
                .commands {
                        MessageCommands()
                }
        }
}
struct MessageCommands : Commands {
        // Try to observe a binding to the key window's `Message` model.
        //
        // In order for this to work, a view in the key window's focused view
        // hierarchy (often the root view) needs to publish a binding using the
        // `View.focusedValue(_:_:)` view modifier and the same `\.message` key
        // path (anologous to a key path for an `Environment` value, defined
        // below).
        @FocusedBinding(\.message) var message: Message?
        
        // FocusedBinding is a binding-specific convenience to provide direct
        // access to a wrapped value.
        //
        // `FocusedValue` is a more general form of the property wrapper, designed
        // to work with all value types, including bindings. The following is
        // functionally equivalent, but places the burden of unwrapping the bound
        // value on the client.
//      @FocusedValue(\.message) var message: Binding<Message>?
        
        var body: some Commands {
                CommandMenu("Message") {
                        Button("Send", action: { message?.send() })
                                .keyboardShortcut("D") // Shift-Command-D
                                .disabled(message?.text.isEmpty ?? true)
                }
        }
}
struct MessageView : View {
        @State var message = Message(text: "Hello, SwiftUI!")
        
        var body: some View {
                TextEditor(text: $message.text)
                        .focusedValue(\.message, $message)
                        .frame(idealWidth: 600, idealHeight: 400)
        }
}
struct Message {
        var text: String
        // ...
        
        mutating func send() {
                print("Sending message: \(text)")
                // ...
        }
}
struct FocusedMessageKey : FocusedValueKey {
        typealias Value = Binding<Message>
}
extension FocusedValues {
        var message: FocusedMessageKey.Value? {
                get { self[FocusedMessageKey.self] }
                set { self[FocusedMessageKey.self] = newValue }
        }
}
//此示例在macOS、iOS和iPadOS上运行。
//
//Big Sur Seed 1存在一些已知的问题,这些问题阻碍了服务器之间的状态共享
//windows和主菜单,因此此示例当前的行为与
//预计在macOS上。此外,Seed中的iOS上禁用了命令API
// 1. 这些问题将在今后的会议上讨论。
//
//所有平台上都提供了聚焦价值API。命令和
//macOS、iOS、iPadOS和Windows上都提供了键盘快捷方式API
//tvOS everywhere键盘输入均可接受。
@主要
结构MessageApp:App{
var body:一些场景{
窗口组{
MessageView()
}
.命令{
MessageCommands()
}
}
}
struct MessageCommands:命令{
//尝试观察键窗口的“消息”模型的绑定。
//
//要使其工作,请在关键点窗口的聚焦视图中创建一个视图
//层次结构(通常是根视图)需要使用
//`View.focusedValue(\:\)`View修饰符和相同的`\.message`键
//路径(与“环境”值的键路径无关,已定义
//下)。
@FocusedBinding(\.message)变量消息:消息?
//FocusedBinding是一种特定于绑定的便利工具,可提供直接
//访问包装的值。
//
//`FocusedValue`是一种更为通用的属性包装形式,专门设计
//使用所有值类型,包括绑定
//在功能上是等效的,但会增加打开边界的负担
//客户机上的价值。
//@FocusedValue(\.message)变量消息:绑定?
var body:一些命令{
命令菜单(“消息”){
按钮(“发送”,操作:{message?.Send()})
.keyboard快捷方式(“D”)//Shift-Command-D
.disabled(message?.text.isEmpty??true)
}
}
}
结构MessageView:View{
@State var message=消息(文本:“您好,SwiftUI!”)
var body:一些观点{
文本编辑器(text:$message.text)
.focusedValue(\.message,$message)
.框架(理想宽度:600,理想高度:400)
}
}
结构消息{
变量文本:字符串
// ...
变异func send(){
打印(“发送消息:\(文本)”)
// ...
}
}
结构FocusedMessageKey:FocusedValueKey{
typealias值=绑定
}
扩展焦点值{
var消息:FocusedMessageKey.Value{
获取{self[FocusedMessageKey.self]}
set{self[FocusedMessageKey.self]=newValue}
}
}

你读过这个吗?他解释得很好。没有,但没有解释如何使用键盘快捷键。给定的示例是可行的,但在其中,如果我尝试使用@FocusedBinding来接收一条消息,该消息包含从变量到WindowGroup的.commands修改器中的更改,则它不起作用。。。