0

添加 Xamarin.Forms.Maps NuGet 包后运行应用程序时出现此错误。我正在尝试在我的 MapPage 中显示地图。我已将名称空间定义添加到我的 XAML 文件中。

我已经添加Xamarin.FormsMaps.Init(this, bundle)到 MainActivity 中的 Android 项目和使用Xamarin.FormsMaps.Init();的 iOS 项目中。在 AppDelegate 文件中。

我搜索了 google 和 stackoverflow,找不到任何有用的文档。

这是 MainPage.xaml 代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TravelRecord"
             x:Class="TravelRecord.MainPage">

    <StackLayout VerticalOptions="Center" Margin="20,0">
        <Entry Placeholder="Enter Your Email... " Keyboard="Email" x:Name="emailEntry" 
               TextColor="{StaticResource blueColor}" Text="Test"/>
        <Entry Placeholder="Enter Your Password... " TextColor="{StaticResource blueColor}"
               IsPassword="True"  x:Name="passwordEntry" Text="Test"/>
        <Button Text="Log in" x:Name="LoginButton" Clicked="LoginButton_Clicked" Margin="0,50,0,0"
                BackgroundColor="{StaticResource blueColor}" TextColor="White"/>
    </StackLayout>

</ContentPage>

和 MainPage.cs 文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace TravelRecord
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void LoginButton_Clicked(object sender, EventArgs e)
        {
           bool emailIsNullOrEmpty =  string.IsNullOrEmpty(emailEntry.Text);
            bool passwordIsNullOrEmpty = string.IsNullOrEmpty(passwordEntry.Text);

            if (emailIsNullOrEmpty || passwordIsNullOrEmpty)
            {

            }

            else
            {

                    Navigation.PushAsync(new HomPage());
            }
        }
    }
}

我使用登录按钮从中导航到 MapsPage。

这是 MapsPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TravelRecord.MapPage"
             xmlns:maps="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps">

    <ContentPage.Content>
        <maps:Map />
    </ContentPage.Content>
</ContentPage>

和 MapsPage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace TravelRecord
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void LoginButton_Clicked(object sender, EventArgs e)
        {
           bool emailIsNullOrEmpty =  string.IsNullOrEmpty(emailEntry.Text);
            bool passwordIsNullOrEmpty = string.IsNullOrEmpty(passwordEntry.Text);

            if (emailIsNullOrEmpty || passwordIsNullOrEmpty)
            {

            }

            else
            {

                    Navigation.PushAsync(new HomPage());
            }
        }
    }
}

这里是android项目的MainActivity.cs:

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.IO;

namespace TravelRecord.Droid
{
    [Activity(Label = "TravelRecord", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            Xamarin.FormsMaps.Init(this, bundle);

            string dataBaseName = "travelRecord.db";
            var dataBaseBath =  System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            string fullPath = Path.Combine(dataBaseBath, dataBaseName);


            LoadApplication(new App(fullPath));
        }
    }
}

和 iOS 项目的 AppDelegate

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Foundation;
using UIKit;

namespace TravelRecord.iOS
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application, as well as listening (and optionally responding) to 
    // application events from iOS.
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();

            Xamarin.FormsMaps.Init();

            string dataBaseName = "travelRecord.db";
            var dataBaseBath =Path.Combine( System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),"..","Library");
            string fullPath = Path.Combine(dataBaseBath, dataBaseName);



            LoadApplication(new App(fullPath));

            return base.FinishedLaunching(app, options);
        }
    }
}

我还在 AndroidManifest.xml 文件中添加了这一行

<meta-data android:name="com.google.android.geo.API_KEY" android:value ="AIzaSyBXuv3VQ4-5pxLcbje-397wvmb3y6RoxsE"></meta-data>

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

4

0 回答 0