You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用SwiftUI Previewer预览LiveActivity?预览报错排查

Live Activity预览报错「Cannot preview this file」求助

根据WWDC 24的内容,所有Live Activity的UI都可通过Previewer可视化,但我目前遇到了「Cannot preview this file」的错误。

我的代码如下:

struct LiveActivityTest: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(for: CustomLAData.self, content: {context in
            VStack{
                Image(systemName: "globe")
                    .resizable()
                Text("Text 1")
            }
        }, dynamicIsland: {context in
            let timeRange = Date()...Date.now+100
            return DynamicIsland{
                DynamicIslandExpandedRegion(.leading, content: {
                    Text("Text 1")
                        .bold()
                })

                DynamicIslandExpandedRegion(.trailing, content: {
                        Text(timerInterval: timeRange, pauseTime: timeRange.lowerBound, countsDown: true)
                            .font(.system(size: 22,weight: .bold))
                            .multilineTextAlignment(.trailing)
                })

                DynamicIslandExpandedRegion(.bottom){
                    Text("Bottom Text")
                }

            } compactLeading: {
                Text("Compact leading")
            } compactTrailing: {
                Image(systemName:"globe")
            } minimal: {
                Image(systemName:"globe")
            }
        })
    }
}

#Preview(as: .dynamicIsland(.compact), using: CustomLAData(), widget: {
    LiveActivityTest()
}, contentStates: {
    CustomLAData.State()
    CustomLAData.State(isLoading:true)
})

struct CustomLAData : ActivityAttributes{
    typealias ContentState = State

    struct State : Codable,Hashable{
        var isLoading = false
    }
}

我尝试过不同的预览宏,但仍未定位到问题。

注:我已将Live Activity Widget添加至Widget Bundle,目标成员设置截图显示:该Widget对应的主App和Widget Extension选项均已勾选。

内容的提问来源于stack exchange,提问作者iroh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 03:02:41