System.Reflection.TargetInvocationException: Исключение было сгенерировано целью вызова?

Я получаю эту ошибку при запуске приложения после добавления пакета NuGet Xamarin.Forms.Maps. Я пытаюсь отобразить карту в моем MapPage. Я добавил определения пространства имен в мой файл XAML.

Я добавил и добавил Xamarin.FormsMaps.Init(this, bundle) в проект Android в MainActivity и в проект iOS с помощью Xamarin.FormsMaps.Init(); в файле AppDelegate.

Я искал Google и Stackru и не мог найти какой-либо полезный документ.

вот код 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());
            }
        }
    }
}

здесь MainActivity.cs проекта android:

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));
        }
    }
}

и AppDelegate проекта iOS

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>

Кто-нибудь знает как это решить? Спасибо

0 ответов

Другие вопросы по тегам