无法隐式转换类型&#39; Java.Lang.Object&#39; to&#39; Android.Runtime.JavaDictionary <string,string =“”>

时间:2018-01-09 03:34:09

标签: c# android xamarin.android

我在我的应用上应用Google Places Api并将Java转换为C#代码。但是当我到达列表中的附近位置时,我收到了这个错误:

无法隐式转换类型&#39; Java.Lang.Object&#39;到&#39; Android.Runtime.JavaDictionary&#39;。存在显式转换(您是否错过了演员?)

private void showNearbyPlaces(JavaList<JavaDictionary<string, string>> placeList)
    {
        for (int eachPlace = 0; eachPlace < placeList.Size(); eachPlace++)
        {

            MarkerOptions markerOptions = new MarkerOptions();
            JavaDictionary<string, string> googlePlace = placeList.Get(eachPlace); /*This is the error*/
        }
    }

代码截图

enter image description here

有谁知道如何解决这个问题?谢谢。

3 个答案:

答案 0 :(得分:0)

尝试明确地投射它:

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

如果这不起作用,我建议将placeList转换为更原始的对象(最有可能的字符串),然后将其重构为字典:

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

public JavaDictionary<string, string> ConvertToString(this [placeListType] placeList)
{
    // Create a dictionary

    // foreach object in placeList
    // Get the first string and assign it
    // Get the second string and assign it
    // Create a new object with them as parameters
    // Add object to dictionary

    // Return dictionary
} 

答案 1 :(得分:0)

您可以使用System.Collections.Generic.List代替JavaList,例如:

using System.Linq;

...

private void showNearbyPlaces(List<JavaDictionary<string, string>> placeList)
{
    for (int eachPlace = 0; eachPlace < placeList.Count; eachPlace++)
    {
        JavaDictionary<string, string> googlePlace = placeList.ElementAt(eachPlace);
    }
}

答案 2 :(得分:0)

像这样使用C#IDictionary,

IDictionary<string, string> placeList= new Dictionary<string, string>();
placeList.Add(new KeyValuePair<string, string>("your string", "your new string"));