【问题标题】:Cannot preview this file - message send failure for display message to agent无法预览此文件 - 向代理显示消息的消息发送失败
【发布时间】:2022-01-12 04:24:59
【问题描述】:

我在 xcode 上使用实时预览时遇到问题。该项目构建良好并在模拟器上运行。我已经连接了所有环境对象,所以我有点困惑。是的,我的应用文件本身也有 EO。

“无法在此文件中预览 - 向代理发送显示消息的消息发送失败”

我在 TabBarView 中的每个视图中都使用 AlertSettingsView - HomeView、SearchView、ExploreView 和 OrdersView。使用 AlertSettings 预览提供程序时,一切正常。但是当我尝试预览整个应用程序时,它不再起作用。如果我删除代码下方突出显示的行,则预览效果会很好。

struct AlertSettingsView: View {
    @EnvironmentObject var avm: AlertsViewModel
    private let width: CGFloat = UIScreen.main.bounds.width
    private let height: CGFloat = UIScreen.main.bounds.height
    var body: some View {
        ZStack {
            Color.theme.orange.ignoresSafeArea()
            VStack {
                ForEach(0..<8) { text in
                    Text("lakjsdfiojas;dfkljadskl;jfj")
                }
            }
        }
        .frame(width: width, height: height/2)
        .animation(.spring())
        .cornerRadius(15)
        .offset(y: avm.settingsViewOffset) <-- This line breaks the preview
    }
}
class AlertsViewModel: ObservableObject {
    @Published var showAlertsView: Bool = true
    @Published var showSettingsView: Bool = false
    @Published var settingsViewOffset: CGFloat = UIScreen.main.bounds.height/2
}
struct TabBarView: View {
    @EnvironmentObject var tvm: TradeViewModel
    @State private var selectedTab: Int = 0
    var body: some View {
        ZStack {
            Color.theme.background.ignoresSafeArea()
            VStack(spacing: -4) {
                ZStack {
                    switch selectedTab {
                    case 0:
                        HomeView()
                    case 1:
                        SearchView()
                    case 2:
                        ExploreView()
                    case 3:
                        OrdersView()
                    default:
                        HomeView()
                    }
                }
                Spacer()
                TabBarButtons(selectedTab: $selectedTab)
            }
            .ignoresSafeArea(.keyboard, edges: .bottom)
            
            HomeMenuView()
        }
        .fullScreenCover(isPresented: $tvm.showTradeView) {
            TradeView()
        }
    }
}

struct TabBarView_Previews: PreviewProvider {
    @State static var avm = AlertsViewModel()
    @State static var hvm = HomeViewModel()
    @State static var ipvm = InvestorProfileViewModel()
    @State static var apvm = ArtistProfileViewModel()
    @State static var tvm = TradeViewModel()
    static var previews: some View {
        TabBarView()
            .environmentObject(avm)
            .environmentObject(hvm)
            .environmentObject(ipvm)
            .environmentObject(apvm)
            .environmentObject(tvm)
            .preferredColorScheme(.dark)
    }
}

【问题讨论】:

  • 此代码无法调试,因为它依赖于您未包含在此处的代码中的类型。
  • 好吧,既然它是一个预览提供程序问题,这在技术上是唯一必要的代码吗?这是我唯一遇到问题的预览版。
  • 除了警报之外,您没有包含任何视图模型。如果您想获得更好的答案,您应该添加一个minimal reproducible example,其他人可以在其中遇到问题。
  • 行尾有个句号,可能是错字,可能是问题

标签: swiftui environmentobject


【解决方案1】:

你有没有试过在 Xcode >> Editor >> Canvas >> "Automatically Refresh Canvas" 下关闭 "Automatically Refresh Canvas" 选项?

--1 月 12 日更新--

在收到来自 Apple 的电子邮件,他们认为这是一个错误,他们需要更多时间/和另一张票来找到解决方法后,我从我的最终找到了一个可重现的临时解决方法。虽然它可能并不适合所有人,但我相信它可以帮助那些使用 M1 Macbook Pro、各种 cocoapod 和不包括 Arm64 架构的人,因为它涉及取消选中/选中“Open with Rosetta”。

请在另一个几乎相同的 SO 问题中查看我的其他答案 -
Cannot preview in this file -- Message send failure

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 2022-11-23
    • 2013-07-23
    • 1970-01-01
    相关资源
    最近更新 更多