【问题标题】:How to take object from one method and pass to another like a parameter on query using xamarin.forms.maps - iOS如何使用 xamarin.forms.maps 从一种方法获取对象并传递给另一种方法,如查询中的参数 - iOS
【发布时间】:2021-07-01 07:01:03
【问题描述】:

我尝试通过此方法在查询中传递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, 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 并像变量 mapCode 一样传递给 DataBaseConnection 方法?

example

【问题讨论】:

    标签: c# ios xamarin maps


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-14
      • 2020-12-07
      • 2017-07-29
      • 1970-01-01
      • 2018-12-24
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多