gpt4 book ai didi

c# - RectForMapRect MonoTouch.UIKit.UIKitThreadAccessException

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:00 26 4
gpt4 key购买 nike

我只在 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 禁用它(在 DEBUG 上);或
  • 使用 --force-thread-check 启用它(在发布时);

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

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com