添加转换功能的功能

时间:2014-02-26 09:56:55

标签: windows-phone-7 ivalueconverter

我正在尝试使用Convert中的IValueConverter函数,但我必须在其中调用另一个函数。我将使用他的返回值,但我得到了错误,告诉我在转换器中返回一个对象值,任何想法我怎么能避免这个。

public void Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    RestClient client = new RestClient();
    client.BaseUrl = "http://";

    RestRequest request = new RestRequest();
    request.Method = Method.GET;
    request.AddParameter("action", "REE");
    request.AddParameter("atm_longitude", location.Longitude);

    client.ExecuteAsync(request, ParseFeedCallBack_ListDistance);
}
public void ParseFeedCallBack_ListDistance(IRestResponse response)
{
    if (response.StatusCode == HttpStatusCode.OK)
    {
        ParseXMLFeedDistance(response.Content);
    }
}
private string ParseXMLFeedDistance(string feed)
{
.... return myvalueToBind;

}

1 个答案:

答案 0 :(得分:0)

计算两个坐标之间距离的简单方法,在这种情况下,假设您拥有设备的坐标,

using System.Device.Location;

public class GeoCalculator
{
    public static double Distance(double deviceLongitude, double deviceLatitude, double atmLongitude, double atmLatitude)
    {
        //Coordinates of ATM (or origin).
        var atmCoordinates = new GeoCoordinate(atmLatitude, atmLongitude);

        //Coordinates of Device (or destination).
        var deviceCordinates = new GeoCoordinate(deviceLatitude, deviceLongitude);

        //Distance in meters.
        return atmCoordinates.GetDistanceTo(deviceCordinates);
    }
}

因此您的转换器可能如下所示:

public class DistanceConverter : IValueConverter
{
    /// <summary>
    /// This is your device coordinate.
    /// </summary>
    private static GeoCoordinate devCoordinate = new GeoCoordinate(61.1631, -149.9721);

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var location = value as LocationModel;

        if (location != null)
        {
            return GeoCalculator.Distance(devCoordinate.Longitude, devCoordinate.Latitude, location.Longitude, location.Latitude);
        }

        return 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

请记住,我个人不会为此使用转换器。我只是在我的模型中公开一个简单的属性来执行此计算,因为它是一个简单的逻辑。如果您恰好是纯粹主义者并且不喜欢模型中的任何逻辑,那么循环遍历列表并在模型上设置属性也会起作用。