c# - RectForMapRect MonoTouch.UIKit.UIKitThreadAccessException

标签 c# ios xamarin.ios thread-safety mapkit

我仅在 MonoTouch 5.4 中遇到此问题。 DrawMapRect是多线程的,但我需要在这里使用RectForMapRect。我可以在没有 InvokeOnMainThread 的情况下做到这一点吗?

错误:

MonoTouch.UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.
  at MonoTouch.UIKit.UIApplication.EnsureUIThread () [0x00019] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:49
  at MonoTouch.MapKit.MKOverlayView.RectForMapRect (MKMapRect mapRect) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/MapKit/MKOverlayView.g.cs:146
  at MapTest.MyMKOverlayView.DrawMapRect (MKMapRect mapRect, Single zoomScale, MonoTouch.CoreGraphics.CGContext context) [0x00009] in /Users/kirill/Desktop/MapTest/MapTest/MapTestViewController.cs:38

源代码在这里:

using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.MapKit;

namespace MapTest
{
    public class MyMKMapView : MKMapView
    {
        public MyMKMapView(RectangleF frame) : base(frame)
        {
            GetViewForOverlay = GetViewForOverlayImp;
        }

        private MKOverlayView GetViewForOverlayImp(MKMapView mapView, NSObject overlay)
        {
            return new MyMKOverlayView();
        }

        public void AddNewOverlay()
        {
            AddOverlay(new MyMKOverlay());
        }
    }

    public class MyMKOverlayView : MKOverlayView
    {
        public MyMKOverlayView() : base()
        {

        }

        public override void DrawMapRect(MKMapRect mapRect, float zoomScale, MonoTouch.CoreGraphics.CGContext context)
        {
            base.DrawMapRect(mapRect, zoomScale, context);
            RectForMapRect(new MKMapRect());
        }
    }

    public class MyMKOverlay : MKOverlay
    {
        public override MKMapRect BoundingMapRect
        {
            get
            {
                return new MKMapRect(10 , 10 , 10 , 10);
            }
        }

        public MyMKOverlay() : base()
        {

        }
    }

    public partial class MapTestViewController : UIViewController
    {
        private MyMKMapView _map;

        public MapTestViewController() : base ("MapTestViewController", null)
        {
            _map = new MyMKMapView(View.Bounds);
            View.AddSubview(_map);
        }

        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();

            // Release any cached data, images, etc that aren't in use.
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
        }

        public override void ViewDidUnload()
        {
            base.ViewDidUnload();

            // Clear any references to subviews of the main view in order to
            // allow the Garbage Collector to collect them sooner.
            //
            // e.g. myOutlet.Dispose (); myOutlet = null;

            ReleaseDesignerOutlets();
        }

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);
            _map.AddNewOverlay();
        }

        public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation)
        {
            // Return true for supported orientations
            return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
        }
    }
}

RectForMapRect(new MKMapRect()) 行中出现错误

最佳答案

这张支票是 new feature MonoTouch 5.4 的。默认情况下,UI 线程检查仅在 DEBUG 版本上启用。您可以在构建时手动:

  • 使用 --disable-thread-check 禁用它(在调试时);或
  • 使用 --force-thread-check 启用它(在发布时);

您还可以使用 CheckForIllegalCrossThreadCalls 在运行时打开/关闭它.

有可能不需要检查,Apple 关于线程安全的文档不是很清楚(与 MSDN 不同)。如果您发现这种情况(或者只是不确定),请填写bug report .

关于c# - RectForMapRect MonoTouch.UIKit.UIKitThreadAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12349463/

相关文章:

ios - 用户跟踪模式缺少动画,Swift

javascript - Bootstrap Popover 点击时关闭 : Works fine in desktop browser but not in mobile browser

url - 使用 Monotouch 在 UILabel 中显示可点击链接的最佳方法

iphone - 在 iPhone 上快速开发应用程序的 Monotouch 还是 Titanium?

ios - 带有文本和图像的 UIBarButtonItem - iOS 8

c# - 使用 Linq-to-XML 和 Linq-to-SQL 生成非常大的 XML 文件

c# - 在后面的代码中隐藏 div 元素

c# - 具有 2 个前端应用程序(Blazor Server 和 Angular)的项目的建议架构,包括 EFCore、Identity、Mediatr

ios - Grand Central Dispatch 嵌套队列和不同的队列优先级

c# - 嵌套的 Json 字符串到 DataTable