我需要创建一个应用程序,当点击注释时,应用程序会从故事板中打开一个新场景 但是当我尝试打开一个新场景时,xamarin会出错。以下是相关代码:
class MyMapDelegate : MKMapViewDelegate
{
string pId = "PinAnnotation";
public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation)
{
// create pin annotation view
MKAnnotationView pinView = (MKPinAnnotationView)mapView.DequeueReusableAnnotation (pId);
if (pinView == null)
pinView = new MKPinAnnotationView (annotation, pId);
((MKPinAnnotationView)pinView).PinColor = MKPinAnnotationColor.Green;
pinView.CanShowCallout = true;
pinView.RightCalloutAccessoryView = UIButton.FromType (UIButtonType.DetailDisclosure);
Console.WriteLine ("anotation view");
return pinView;
}
public override void CalloutAccessoryControlTapped (MKMapView mapView, MKAnnotationView view, UIControl control)
{
AboutPodnik aboutPodnik = Storyboard.InstantiateViewController ("AboutPodnik") as AboutPodnik; //cannot acces non-static member
NavigationController.PushViewController (aboutPodnik, true); //cannot acces non-static member
}
}
这是我得到的错误:
Cannot access a nonstatic member of outer type `MonoTouch.UIKit.UIViewController' via nested type `Projekt.Mapa.MyMapDelegate'
答案 0 :(得分:0)
您收到错误是因为您无法访问地图视图委托中所需的Storyboard对象,因为您位于视图控制器之外,因此您必须以某种方式返回"点击"到视图控制器上下文。这是一种方法:
在MyMapDelegate
课程中:
class MyMapDelegate : MKMapViewDelegate
{
public event EventHandler AnnotationTapped;
public override void CalloutAccessoryControlTapped(MKMapView mapView, MKAnootationView view, UIControl control)
{
if (AnnotationTapped != null) {
AnnotationTapped(view, new EventArgs());
}
}
}
然后,在您的视图控制器中:
public override void ViewDidLoad()
{
base.ViewDidLoad();
var mapViewDelegate = new MyMapDelegate();
mapViewDelegate.AnnotationTapped += TheMapView_OnAnnotationTapped;
// Replace 'TheMapView' with whatever your MKMapView control is named
TheMapView.Delegate = mapViewDelegate;
}
private void TheMapView_OnAnnotationTapped(object sender, EventArgs args)
{
AboutPodnik aboutPodnik = Storyboard.InstantiateViewController ("AboutPodnik") as AboutPodnik;
NavigationController.PushViewController (aboutPodnik, true);
}