c# - 从 Xamarin 绑定(bind)调用 UIKit 方法

标签 c# ios xamarin.ios xamarin uikit

我的 Xamarin 项目有一个广告 SDK 绑定(bind)。绑定(bind)在这里: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);
}

但是,当加载 View 时,出现错误:

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

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

最佳答案

您正在调用的 UIKIt 方法是 UIScreen.MainScreen。

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

关于c# - 从 Xamarin 绑定(bind)调用 UIKit 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26902207/

相关文章:

iphone - 原力风景 iOS 6

xamarin.ios - 如何以编程方式从 MonoTouch 中的 ViewController 加载另一个 viewController

javascript - 如何通过 ExecuteScript 添加自己的监听器来捕获 "load"事件?

ios - 获取 JSON 值到数组

ios - 在 iOS 中访问本地存储的(对用户隐藏,在库下)文件

xamarin - Xamarin.iOS 中的抽屉导航

C# Xamarin 避免代码重复

c# - .NET C# Dataset WriteXml 包含/排除列并添加命名空间

c# - 程序集可以没有版本吗?

c# - 锁定 Windows Phone 模拟器