Search code examples
objectdictionarywindows-phone-8collectionssend

Windows phone 8: How to send Object to an Event?


I'm developing an taxi caller app. And I used Bing map enter image description here

This is my flow:

    1. Get my position
    2. Get 3 near taxis
    3. When I tap 1 in 3 taxis, all in formation of taxis will sent to a new

Variable to used with another function.

This is my code And how to send a obj from Foreach to taxiIcon_Tap?

Thanks and best regards!

    //------ BEGIN get near Driver ------//
    private async void GetNearDriver()
    {
        var uid = userData.content.uid;
        var lat = pickupLat;
        var lng = pickupLng;
        var clvl = taxiType;

        var input = string.Format("{{\"uid\":\"{0}\",\"lat\":{1},\"lng\":{2},\"cLvl\":\"{3}\"}}", uid, lat.ToString().Replace(',', '.'), lng.ToString().Replace(',', '.'), clvl);
        var output = await GetJsonFromPOSTMethod.GetJsonString(ConstantVariable.tNetRiderGetNerDriverAddress, input);
        var nearDriver = JsonConvert.DeserializeObject<RiderGetNearDriver>(output);
        if (nearDriver.content.listDriverDTO.Count > 0)
        {
            foreach (var taxi in nearDriver.content.listDriverDTO)
            {
                ShowNearDrivers(taxi.lat, taxi.lng, taxi.cName);
            }
        }
    }
    //------ END get near Driver ------//


    //------ BEGIN show and Design UI 3 taxi near current position ------//
    private void ShowNearDrivers(double lat, double lng, string tName)
    {            
        GeoCoordinate TaxiCoordinate = new GeoCoordinate(lat, lng);

        //Create taxi icon on map
        Image taxiIcon = new Image();
        taxiIcon.Source = new BitmapImage(new Uri("/Images/Taxis/img_CarIcon.png", UriKind.Relative));

        //Add a tapped event
        taxiIcon.Tap += taxiIcon_Tap;

        //Create Taxi Name 
        TextBlock taxiName = new TextBlock();
        taxiName.HorizontalAlignment = HorizontalAlignment.Center;
        taxiName.Text = tName;
        taxiName.FontSize = 12;
        taxiName.Foreground = new SolidColorBrush(Color.FromArgb(255, (byte)46, (byte)159, (byte)255)); //RBG color for #2e9fff

        //Create Stack Panel to group icon, taxi name, ...            
        Rectangle taxiNameBackground = new Rectangle();
        taxiNameBackground.Height = 18;
        taxiNameBackground.Width = taxiName.ToString().Length + 20;
        taxiNameBackground.RadiusX = 9;
        taxiNameBackground.RadiusY = 7;
        //taxiNameBackground.Stroke = new SolidColorBrush(Color.FromArgb(255, (byte)171, (byte)171, (byte)171)); //RBG color for #ababab
        taxiNameBackground.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)213, (byte)235, (byte)255)); //RBG color for #d5ebff

        Grid taxiNameGrid = new Grid();
        taxiNameGrid.Margin = new Thickness(0, 4, 0, 4); //Margin Top and Bottom 4px
        taxiNameGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
        taxiNameGrid.VerticalAlignment = System.Windows.VerticalAlignment.Center;
        taxiNameGrid.Children.Add(taxiNameBackground);
        taxiNameGrid.Children.Add(taxiName);

        StackPanel taxiStackPanel = new StackPanel();
        //taxiStackPanel.Margin  = new Thickness(5, 0, 5, 0);
        taxiStackPanel.Children.Add(taxiIcon);
        taxiStackPanel.Children.Add(taxiNameGrid);

        // Create a MapOverlay to contain the circle.
        MapOverlay myTaxiOvelay = new MapOverlay();
        //myTaxiOvelay.Content = myCircle;
        myTaxiOvelay.Content = taxiStackPanel;
        myTaxiOvelay.PositionOrigin = new Point(0.5, 0.5);
        myTaxiOvelay.GeoCoordinate = TaxiCoordinate;

        //Add to Map's Layer
        riderMapLayer = new MapLayer();
        riderMapLayer.Add(myTaxiOvelay);

        map_RiderMap.Layers.Add(riderMapLayer);
    }

    //Tapped event
    private void taxiIcon_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        //Hide Step 01
        this.grv_Step01.Visibility = Visibility.Collapsed;

        //Show Step 02
        this.grv_Step02.Visibility = Visibility.Visible;
        this.grv_Picker.Visibility = Visibility.Collapsed;
        //Step 2 info
        LoadStep2Info();
    }
    //------ END show and Design UI 3 taxi near current position ------//

Solution

  • You can use the Tag property to pass such miscellaneous objects around when using events which don't expose the ability to add an additional payload.

    My understanding is that in the taxiIcon_Tap event you want to be able to access your own object that is related to whatever was tapped.
    To do this set you object to the Tag property of the item being tapped.

    i.e. If you want to pass the coordinates do:

    taxiIcon.Tag = TaxiCoordinate;
    

    (You could pass anything.)

    Then, in the tapped event handler you can get at this object by casting from the sender to get the Tag and then casting that back to your type.

    var coords = ((FrameworkElement)sender).Tag as GeoCoordinate;