2014-02-26 101 views
0

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

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; 

}

+0

接口“System.Windows.Data.IValueConverter”期望您为两个方法Convert和ConvertBack提供实现,两者都需要您返回一个对象。无效方法无效。您必须至少返回null并将您的convert方法更改为“public object Convert”。作为一个侧面说明,你到底想要达到什么目标?在这种情况下使用转换器似乎是错误的方法,我显然无法判断,因为您没有提供关于常见问题的上下文。 – FunksMaName

+0

事实是,我需要在我的列表框中使用经度和纬度的foreach项目,并使用它们来调用web服务以获得设备和该项目之间的距离..因此,我使用了这种方法,您有任何其他建议吗? –

+0

在这种情况下,你最好在本地进行计算。你有设备坐标和一个参考坐标。可以为列表中的每个项目打开x个HTTP连接,从而节省设备电池和数据使用量。看到下面的响应 – FunksMaName

回答

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(); 
    } 
} 

请记住,我个人不会使用此转换器。我只是在我的模型中公开一个简单的属性,因为它是一个简单的逻辑。如果你碰巧是一个纯粹主义者,并且不喜欢你模型中的任何逻辑,循环遍历列表并在模型上设置一个属性也可以。

+0

这工作正常,但我使用的web服务,其中我通过atms和位置设备的感情,然后我得到了一个结果返回它,就像我在我的代码之前解释 –

+0

任何想法如何我可以避免解析的返回函数来获取它在转换方法吗? –