从 Xamarin Binding 调用 UIKit 方法

本文关键字:UIKit 方法 调用 Binding Xamarin | 更新日期: 2023-09-27 18:30:22

我有一个用于我的 Xamarin 项目的播发 SDK 绑定。绑定在这里:https://github.com/bbhsu2/XamarinAdMarvelBinding

我的问题来自方法:

[Export("adMarvelViewFrame")]
RectangleF AdMarvelViewFrame();

在我的类中,我这样实现它:

[Export("adMarvelViewFrame")]
public RectangleF AdMarvelViewFrame()
{
    return new RectangleF(0.0f, 0.0f, UIScreen.MainScreen.Bounds.Width, 250.0f);
}

但是,当视图加载时,我收到错误:

UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.

(认为)我理解这个错误的含义,但是当本机代码在其他地方调用它时,我将如何在主线程上调用它?

从 Xamarin Binding 调用 UIKit 方法

你调用的UIKIt方法是UIScreen.MainScreen。

简单的解决方法是在类变量中预取UIScreen.MainScreen,然后在AdMarvelViewFrame中使用类变量。