0

当我测试这段代码时:

Console.WriteLine(SwearJarController.GetLocation());

我得到错误:

SwearJarController 指的是一个单独的项目,下面是相关代码:

static string baseUri = "http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false";

    public static string GetLocation()
    {
        GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
        var myPosition = watcher.Position;
        double latitude = -45.856633;
        double longitude = 170.500646;

        if (!myPosition.Location.IsUnknown)
        {
            latitude = myPosition.Location.Latitude;
            longitude = myPosition.Location.Longitude;
        }

        RetrieveFormatedAddress(latitude, longitude);



        return currentLocation;
    }


    public static void RetrieveFormatedAddress(double latitude, double longitude)
    {
        string strlat = Convert.ToString(latitude);
        string strlon = Convert.ToString(longitude);

        string requestUri = string.Format(baseUri, strlat, strlon);


        WebClient wc = new WebClient();
        {
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri(requestUri));
        }

    }


    public static string currentLocation = "Unknown";

    static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        try
        {
            var xmlElm = XElement.Parse(e.Result);
            var status = (from elm in xmlElm.Descendants()
                          where elm.Name == "status"
                          select elm).FirstOrDefault();
            if (status.Value.ToLower() == "ok")
            {
                var res = (from elm in xmlElm.Descendants()
                           where elm.Name == "formatted_address"
                           select elm).FirstOrDefault();
                currentLocation = res.Value;
            }
            else
            {
                currentLocation = "Unknown";
            }
        }
        catch
        {
            currentLocation = "Unknown";
        }
    }

这可能是因为控制器是 WP7 应用程序,但测试器是控制台应用程序?

谢谢!

4

1 回答 1

0

是的。正因为如此,但在这里你有如何从控制台尝试(单元测试方式)。

基本上您需要更改ProjectTypeGuids节点以匹配 WP7 应用程序,保存文件然后重新加载项目。将节点更改为:

<ProjectTypeGuids>
    {C089C8C0-30E0-4E22-80C0-CE093F111A43};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}
</ProjectTypeGuids>

然后您可以成功引用 WP7 程序集。

于 2012-09-14T22:20:50.757 回答