微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 xamarin.forms.maps 从一种方法中获取对象并像参数一样传递给另一种方法 - iOS

如何解决如何使用 xamarin.forms.maps 从一种方法中获取对象并像参数一样传递给另一种方法 - iOS

我尝试通过此方法查询时传递类似 CodeNum 对象的参数:

protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView,IMKAnnotation annotation)
    {
        MKAnnotationView annotationView = null;

        if (annotation is MKUserLocation)
            return null;


        var customPin = GetCustomPin(annotation as MKPointAnnotation);
        if (customPin == null)
        {
            throw new Exception("Custom pin not found");
        }

        annotationView = mapView.DequeueReusableAnnotation(customPin.Name);
        if (annotationView == null)
        {
            annotationView = new CustomMKAnnotationView(annotation,customPin.Name);
            annotationView.CalloutOffset = new CGPoint(0,0);
            ((CustomMKAnnotationView)annotationView).Name = customPin.Name;
            ((CustomMKAnnotationView)annotationView).Url = customPin.Url;
            ((CustomMKAnnotationView)annotationView).Address = customPin.Address;
            //Add First Line
            ((CustomMKAnnotationView)annotationView).AlertLevel = customPin.AlertLevel;
            
            if (customPin.AlertLevel == 1)
            {
                annotationView.Image = UIImage.FromFile("green.png");
            }
            else if (customPin.AlertLevel == 2)
            {
                annotationView.Image = UIImage.FromFile("yellow.png");
            }
            else if (customPin.AlertLevel == 3)
            {
                annotationView.Image = UIImage.FromFile("orange.png");
            }
            else if (customPin.AlertLevel == 4)
            {
                annotationView.Image = UIImage.FromFile("red.png");
            }
            
            //Add Second Line
            ((CustomMKAnnotationView)annotationView).CodeNum = customPin.CodeNum;  
        }
       
        annotationView.CanShowCallout = true;

        configureDetailView(annotationView);

        return annotationView;
    }

用户点击地图上的某个图钉以获取 CodeNum 并传递给查询以从数据库获取数据时。如何将此参数传递给 OnDidSelectAnnotationView 方法

 void OnDidSelectAnnotationView(object sender,MKAnnotationViewEventArgs e)
    {
        var customPin = GetCustomPin(annotation as MKPointAnnotation);
        var result = DataBaseConnection(customPin.CodeNum);
        MessagingCenter.Send<object,IEnumerable<AlertLevel>>(this,"PinSelected",result);

        CustomMKAnnotationView customView = e.View as CustomMKAnnotationView;
        customPinView = new UIView();

        if (customView.Name.Equals("Xamarin"))
        {
            customPinView.Frame = new CGRect(0,200,84);

            customPinView.Center = new CGPoint(0,-(e.View.Frame.Height + 75));

            e.View.AddSubview(customPinView);
        }
    }

OnDidSelectAnnotationView 方法中,这行代码出现错误

var customPin = GetCustomPin(annotation as MKPointAnnotation);

错误 CS0103:名称“注释”在当前上下文中不存在 (CS0103)

我的 GetCustomPin 方法如下所示:

CustomPin GetCustomPin(MKPointAnnotation annotation)
    {
        var position = new Position(annotation.Coordinate.Latitude,annotation.Coordinate.Longitude);

        foreach (var pin in customPins)
        {
            if (pin.Position == position)
            {
                return pin;
            }
        }
        return null;
    }

这是我连接数据库并返回列表的方法

public IEnumerable<AlertLevel> DataBaseConnection(int mapCode)
    {
        string ConnectionString = "server=192.168.1.2;uid=UName;port=4443;pwd=Password;database=dbname;";
        MysqLConnection Conn = new MysqLConnection(ConnectionString);
        var listAlert = new List<AlertLevel>();

        try
        {
            Conn.open();

            //replace(2) with mapCode
            string query = "CALL Get_Alert_levels_Station(" + mapCode + ");";
            MysqLCommand myCommand = new MysqLCommand(query,Conn);
            MysqLDataReader myReader;

            myReader = myCommand.ExecuteReader();

            try
            {
                while (myReader.Read())
                {
                    var currentData = new AlertLevel()
                    {

                        dateForecast = myReader.GetDateTime(0),levelForecast = myReader.GetInt32(1)

                    };

                    listAlert.Add(currentData);
                }
            }
            finally
            {
                myReader.Close();
                Conn.Close();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Database Connection","Not Connected ..." + Environment.NewLine + ex.ToString(),"OK");
        }

        return listAlert;
    }

如何从点击的 pin 中获取 CodeNum 并像变量 DataBaseConnection 一样传递给 mapCode 方法

example

解决方法

可以使用 MessagingCenter 发送消息 您可以通过以下链接使用 MessagingCenter MessagingCenter

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?