Paul Gordon
Paul Gordon

Reputation: 1

how to Return an object or more than one value from a converter

hey guys as the title state, im trying to return both logitude and lattiude from my mapclicked event to my view model. here my attempt, but it didnt work.

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{

var mapClickedEventArgs = value as Xamarin.Forms.Maps.MapClickedEventArgs;

var currentPossition = mapClickedEventArgs;

var lati = currentPossition.Position.Latitude;

var longi = currentPossition.Position.Longitude;



return lati;

}

Upvotes: 0

Views: 278

Answers (1)

Misha Zaslavsky
Misha Zaslavsky

Reputation: 9712

You can use 2 good approaches:

  1. You can return a value tuple:
public (double lati, double longi) Convert(
    object value, Type targetType, object parameter, CultureInfo culture)

{
    var mapClickedEventArgs = value as Xamarin.Forms.Maps.MapClickedEventArgs;

    var currentPossition = mapClickedEventArgs;

    var lati = currentPossition.Position.Latitude;
    var longi = currentPossition.Position.Longitude;

    return (lati, longi);
}

Note, I wasn't sure what is the data type of lati & longi so I assumed it is of type double. In case it is a different type, please change the return values from double to the relevant type.

  1. Return the Position class or create your own class that will include the properties lati & longi and instead of returning an object, return this class. The last row will be something like:
return currentPosition.Position;

Or:

return new YourClassName(lati, longi);

UPDATE:

Maybe, I didn't understand the question right. Maybe you can't change the method signature.

If this is the case, you can just return it using anonymous types:

return new { lati, longi };

Upvotes: 1

Related Questions