sending wrong data

Eduardo Gomez Romero 705 Reputation points
2024-09-23T02:01:52.36+00:00

I have two classes


    public class Turbine {

        public int Id { get; set; }

        public string? Name { get; set; }

        public string? Label { get; set; }

        public string? Address { get; set; }

        public Location? Location { get; set; }

        public ObservableCollection<string>? Images { get; set; }

        public ObservableCollection<TurbineData>? DataCollection { get; set; }

    }
}


    public class TurbinePin {
        public ICommand? PinClickedCommand { get; set; }
        public Turbine? Turbine { get; set; }
    }

I also created a custom control, since the map doesn't have a command for the pins


namespace METROWIND.Controls {

    public class CustomMapPin : Pin {

        public static readonly BindableProperty MarkerClickedCommandProperty =
            BindableProperty.Create(
                nameof(MarkerClickedCommand),
                typeof(ICommand),
                typeof(CustomMapPin),
                null);

        public ICommand? MarkerClickedCommand {
            get => (ICommand)GetValue(MarkerClickedCommandProperty);
            set => SetValue(MarkerClickedCommandProperty, value);
        }





        public static readonly BindableProperty CustomPinLabelProperty = BindableProperty.Create(
            nameof(CustomPinLabel), typeof(string), typeof(CustomMapPin));

        public string CustomPinLabel {
            get => (string)GetValue(CustomPinLabelProperty);
            set => SetValue(CustomPinLabelProperty, value);
        }


        public static readonly BindableProperty CustomPinAddressProperty = BindableProperty.Create(
            nameof(CustomPinAddress), typeof(string), typeof(CustomMapPin));

        public string CustomPinAddress {
            get => (string)GetValue(CustomPinAddressProperty);
            set => SetValue(CustomPinAddressProperty, value);
        }


        public static readonly BindableProperty TurbineImagesProperty = BindableProperty.Create(
            nameof(TurbineImages),
            typeof(IEnumerable),
            typeof(CustomMapPin));

        public IEnumerable TurbineImages {
            get => (IEnumerable)GetValue(TurbineImagesProperty);
            set => SetValue(TurbineImagesProperty, value);
        }

        public static readonly BindableProperty ChartDataProperty = BindableProperty.Create(
            nameof(ChartData), typeof(IEnumerable),
            typeof(CustomMapPin));

        public IEnumerable ChartData {
            get => (IEnumerable)GetValue(ChartDataProperty);
            set => SetValue(ChartDataProperty, value);
        }


        public static readonly BindableProperty TurbineProperty = BindableProperty.Create(
            nameof(Turbine), typeof(Turbine), typeof(CustomMapPin));

        public Turbine Turbine {

            get => (Turbine)GetValue(TurbineProperty);
            set => SetValue(TurbineProperty, value);
        }

        public CustomMapPin() {
            MarkerClicked += CustomPin_MarkerClicked;
        }

        private void CustomPin_MarkerClicked(object? sender, PinClickedEventArgs e) {
            // Ensure info window is shown
            e.HideInfoWindow = true;

            // Execute command if any
            if (MarkerClickedCommand?.CanExecute(this) == true) {
                MarkerClickedCommand.Execute(this);
            }
        }
    }
}

and I have my vm

        public ChargingStationsMapPageViewModel() {

            OnPinMarkerClickedCommand = new Command<object>(OnPinMarkerClicked);

            Turbines = TurbinesService.GetTurbinePins(OnPinMarkerClickedCommand);
        }

        [RelayCommand]
        private void Appearing(Microsoft.Maui.Controls.Maps.Map map) {

            if (map != null) {

                MapView = map;
            }
        }

        [RelayCommand]
        void ItemSelected(Turbine Turbine) {

            var mapSpan = MapSpan.FromCenterAndRadius(
                Turbine.Location!,
                Distance.FromKilometers(0.4));

            MapView!.MoveToRegion(mapSpan);
            IsExpanded = false;
        }

        void OnPinMarkerClicked(object turbine) {
            if (turbine != null) {
                // Handle the pin click event
                Shell.Current.GoToAsync($"{nameof(TurbineDetailPage)}",
                    true,
                    new Dictionary<string, object> {
                    { "SelectedTurbine", turbine }
                });
            };
        }

        [RelayCommand]
        void OpenMenu() {

            IsOptionsOpen = true;
        }

        [RelayCommand]
        void ChangeMapType(int mapType) {

            if (mapType == 0) {

                MapView!.MapType = MapType.Street;

            } else {

                MapView!.MapType = MapType.Satellite;
            }

            IsOptionsOpen = false;
        }
    }
}
<maps:Map
    x:Name="ChargingStationMap"
    ItemsSource="{Binding Turbines}"
    VerticalOptions="FillAndExpand">
    <maps:Map.ItemTemplate>
        <DataTemplate x:DataType="model:TurbinePin">
            <controls:CustomMapPin
                Address="{Binding Turbine.Address}"
                ChartData="{Binding Turbine.DataCollection}"
                CommandParameter="{Binding Turbine}"
                Label="{Binding Turbine.Label}"
                Location="{Binding Turbine.Location}"
                MarkerClickedCommand="{Binding PinClickedCommand}"
                Turbine="{Binding Turbine}"
                TurbineImages="{Binding Turbine.Images}" />
        </DataTemplate>
    </maps:Map.ItemTemplate>
</maps:Map>

but when I click a pin, I am trying to receive the Turbine object

but I am getting the

User's image

the customMap

I already tried to change my command to <Turbine> but is always the same (I just put object, to make sure it was hitting the breakpoint, because command are picky and if you do not send what they want they do not work)

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,541 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,001 Reputation points Microsoft Vendor
    2024-09-23T09:10:11.9233333+00:00

    Hello,

    If you want to get the Turbine, and you get the CustomMapPin now, you can get the selected Turbine by var selectedTurbine = customMapPin.Turbine; like following code.

    void OnPinMarkerClicked(object MapPin)
    
    {
    
        if (MapPin != null)
    
        {
    
            CustomMapPin customMapPin= MapPin as CustomMapPin;
    
           var selectedTurbine = customMapPin.Turbine;
    
            // Handle the pin click event
    
            Shell.Current.GoToAsync($"{nameof(TurbineDetailPage)}",
    
                true,
    
                new Dictionary<string, object> {
    
                { "SelectedTurbine", selectedTurbine }
    
            });
    
        };
    
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.