首页 > 解决方案 > 无法将 xamarin.forms.map 控件添加到 Xamarin Form

问题描述

我想向我的 android 应用程序添加地图控件。为此,我从 Nuget 获得了最新的 xamarin.forms 和 xamarin.forms.maps 包。

我的逻辑代码什么都不做,我的 axml 页面如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
        <LinearLayout
            android:orientation="vertical"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/optionsContainerLinearLayout">
           //Other UI elements
        <maps:Map>
        </maps:Map>
        </LinearLayout>
...
</LinearLayout>

注意我在 maps:Map 中没有标签,以便提供最小的错误机会。请注意,当我直接在下面使用更多属性填充此地图时,代码仍然以相同的方式中断:

<map:Map
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mockMapView">
</map:Map>

当我运行此应用程序时,它会SetContentView()因错误而中断:

Didn't find class "android.view.Map" on path: DexPathList
[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.geosolve.pavestate2-fJtHBLcT2gY6AlUrxcOOVg==/base.apk"],
nativeLibraryDirectories=[/data/app/com.geosolve.pavestate2-fJtHBLcT2gY6AlUrxcOOVg==/lib/x86, /data/app/com.geosolve.pavestate2-fJtHBLcT2gY6AlUrxcOOVg==/base.apk!/lib/x86, /system/lib]]

我下载了另一个示例 xamarin 应用程序,它在我的模拟器上运行良好,所以我认为模拟器不是问题所在。我的 API 密钥也被其他应用程序使用,所以我认为 API 密钥也不是问题。

我的 androidManifest.xml 看起来像这样:

...
    <application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:roundIcon="@mipmap/icon" android:supportsRtl="true" android:theme="@style/GeosolveApp">
        <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="(My API Key)" />
    <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    </application>
...

例外

关于为什么会这样的任何帮助;不只是像您期望的那样出现吗?

标签: androidxamarinxamarin.formsdexxamarin.forms.maps

解决方案


对于 Xamarin.Android,我们通常从 NuGet 安装 Xamarin.GooglePlayServices.Maps 包

并将地图添加到您的 xaml 中,例如:

声明式

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/map"
   class="com.google.android.gms.maps.SupportMapFragment" /> 

以编程方式

var mapFrag = MapFragment.NewInstance();
activity.FragmentManager.BeginTransaction()
                        .Add(Resource.Id.map_container, mapFrag, "map_fragment")
                        .Commit();

你越能阅读谷歌地图


推荐阅读