System.Reflection.TargetInvocationException:调用的目标是否引发了异常?

时间:2018-09-02 11:23:32

标签: xamarin.forms system.reflection targetinvocationexception

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

我已经并且已经使用Xamarin.FormsMaps.Init(this, bundle)Xamarin.FormsMaps.Init()添加到MainActivity中的Android项目以及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>
  

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

0 个答案:

没有答案