如何实现谷歌将api放入Xamarin.Android

时间:2018-01-15 01:08:05

标签: xamarin xamarin.android google-places-api

我在我的应用上应用Google Places Api并将Java转换为C#代码。但我得到了这个错误。

enter image description here

enter image description here

提前谢谢。

1 个答案:

答案 0 :(得分:1)

  

无法将类型'Java.Lang.Object'隐式转换为'Android.Runtime.JavaDictionary'。存在显式转换(您是否错过了演员?)

我注意到在previous question中,placeList的类型为JavaList, 您应该注意JavaList.Get(position)返回Java.Lang.Object的类型。您无法直接将其转换为JavaDictionary

JavaList Get(int location)方法:

public class JavaList : Java.Lang.Object, System.Collections.IList, System.Collections.ICollection, IEnumerable
{
    ...
    public virtual Java.Lang.Object Get(int location);
}

解决方案1:

正如@Jon Douglas所说,你可以使用JavaCast

JavaDictionary<string, string> googlePlace = placeList.Get(eachPlace).JavaCast<JavaDictionary<string, string>>();

解决方案2:

您可以使用System.Linq

JavaDictionary<string, string> googlePlace = placeList.ElementAt(eachPlace);

完整代码:

using System.Linq;
...
private void showNearbyPlaces(JavaList<JavaDictionary<string, string>> placeList)
{
    for (int eachPlace = 0; eachPlace < placeList.Count; eachPlace++)
    {       
        JavaDictionary<string, string> googlePlace = placeList.ElementAt(eachPlace);

        string reference = googlePlace["reference"];
        double lat = System.Double.Parse(googlePlace["lat"]);
    }
}