Home » c# » c# – How to set point (x,y) in xaml page

c# – How to set point (x,y) in xaml page

Posted by: admin February 21, 2020 Leave a comment

Questions:

I have created a struct class Position as like Point struct.

In C#, I set the Position like Position Northeast = new Position(13.084306, 80.210024);

public struct Position 
{
    public double Latitude { get; set; }

    public double Longitude { get; set; }

    public Position(double latitude, double longitude)
    {
        Latitude = latitude;
        Longitude = longitude;
    }
}

But I don’t know how to set this in XAML like <maps:GeoBounds Northeast="9,9">

I want the implicit conversion within the Position struct.

Anyone please suggest the solution how to I convert the Position points as like .Net struct Point.

How to&Answers:

XAML element syntax should work:

<maps:GeoBounds>
    <maps:GeoBounds.Northeast>
        <maps:Position Latitude="9" Longitude="9"/>
    </maps:GeoBounds.Northeast>
</maps:GeoBounds>

You may also write a TypeConverter that converts from string to Position:

public class PositionConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        return Position.Parse((string)value);
    }
}

Register it with the Position type like this:

[System.ComponentModel.TypeConverter(typeof(PositionConverter))]
public struct Position 
{
    ...

    public static Position Parse(string positionString)
    {
        ...
    }
}

Now attribute syntax like

<maps:GeoBounds Northeast="9,9">

would pass the string "9,9" to the Parse method, where you return an appropriate Position instance.