EventToCommandBehavior exception for MarkerSelected event

I tried to use RelayCommand and EventToCommandBehavior with sfMaps with MarkedSelected event but it throws and exception. (code-behind works fine of course)

Similar code for sfTabView works fine for the SelectionChanged event.

If the sfMaps EventToCommandBehavior code in xaml is uncommented you can see the exception.

I'm attaching a sample code.

Thanks for your help



Attachment: MauiAppBehaviorTest_9745119e.zip

3 Replies

VO Vishal Omprasad Syncfusion Team April 1, 2025 11:19 AM UTC

Hi George,

We have reviewed your query regarding the "EventToCommandBehavior exception for the MarkerSelected event" in SfMaps control. After analyzing your sample, we found that the EventToCommandBehavior was implemented on the SfMaps control instead of the SfMaps.MapTileLayer resulting in the TargetInvocationException.

Currently, the .NET MAUI SfMaps control does not directly support EventToCommandBehavior for MapLayer. However, you can still achieve the desired functionality by implementing a custom EventToCommandBehavior on the MapTileLayer, as shown in the following code snippet.

[Custom EventToCommandBehavior class]:

public static class EventToCommandBehavior

{

    public static readonly BindableProperty CommandProperty =

        BindableProperty.CreateAttached(

            "Command", typeof(ICommand), typeof(EventToCommandBehavior), null, propertyChanged: OnCommandChanged);

 

    public static ICommand GetCommand(BindableObject view) => (ICommand)view.GetValue(CommandProperty);

    public static void SetCommand(BindableObject view, ICommand value) => view.SetValue(CommandProperty, value);

 

    private static void OnCommandChanged(BindableObject bindable, object oldValue, object newValue)

    {

        if (bindable is MapLayer layer)

        {

            if (layer != null)

            {

                layer.MarkerSelected -= OnMarkerSelected;

                if (newValue is ICommand command)

                {

                    layer.MarkerSelected += (s, e) =>

                    {

                        command.Execute(e.SelectedMarker);

                    };

                }

            }

        }

    }

 

    private static void OnMarkerSelected(object? sender, MarkerSelectedEventArgs e)

    {

        var layer = sender as MapLayer;

        if (layer != null)

        {

            var command = GetCommand(layer);

            if (command?.CanExecute(e.SelectedMarker) == true)

            {

                command.Execute(e.SelectedMarker);

            }

        }

    }

}

[ViewModel]:

public ICommand MarkerSelectedCommand1 { get; }

 

public SecondPageViewModel()

{

    MarkerSelectedCommand1 = new Command<MapMarker>(OnMarkerSelected);

}

 

private void OnMarkerSelected(MapMarker selectedMarker)

{

    var marker = selectedMarker;

    if (marker != null)

    {

        System.Diagnostics.Debug.WriteLine($"Marker selected: Latitude = {marker.Latitude}, Longitude = {marker.Longitude}");

    }

}

[XAML]:

<maps:SfMaps x:Name="map" VerticalOptions="Fill" Margin="3" >

    <maps:SfMaps.Layer>

        <maps:MapTileLayer x:Name="tileLayer"

                           UrlTemplate="https://tile.openstreetmap.org/{z}/{x}/{y}.png"

                           Markers="{Binding Markers}"

                           viewmodel:EventToCommandBehavior.Command="{Binding MarkerSelectedCommand1}">

 

        </maps:MapTileLayer>

    </maps:SfMaps.Layer>

</maps:SfMaps>

We have modified your provided sample, as illustrated in the above approach, and attached it below for your reference. Please check the sample and let us know whether your requirement is achieved.

If you have any further questions or require additional assistance, please don't hesitate to reach out. We are here to help.

Regards,
Vishal O.


Attachment: MauiAppBehaviorTest_56e91d43.zip


GD George DAlessio April 1, 2025 03:01 PM UTC

Thanks for your prompt reply. That works although I was trying to use the RelayCommand as in the SfTabView and you mention that the SfMaps doesn't yet directly support EventToCommandBehavior.

Until now I have being subclassing Behavior<SfMaps> to achieve the desired functionality like the ThirdPage code shows in the modified project included, but I was hoping to use RelayCommand directly without the need to subclass. 

Thanks for your help

 


Attachment: MauiAppBehaviorTest_86b63764.zip


VO Vishal Omprasad Syncfusion Team April 4, 2025 01:28 PM UTC

Hi George,

As of now, the .NET MAUI SfMaps control does not have “Command support for MarkerSelected event”. We have logged a feature request for this enhancement.

We plan to implement this feature in any of our upcoming releases. During the planning stage for each release cycle, we review all open features and identify implementations based on parameters such as product vision, technological feasibility, and customer interest. Please upvote this feature to prioritize it.

We will notify you when this feature is implemented. However, you can communicate with us regarding any open features at any time using our Feature Report page.

Feedback link: https://www.syncfusion.com/feedback/66751/command-support-for-markerselected-event-in-sfmaps-control.

If you have any more specifications/suggestions to the feature request, you can add it as a comment in the portal and cast your vote to make it count.

Regards,
Vishal O.


Loader.
Up arrow icon