InitializeComponent和map变量抛出运行时错误

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

标签: xamarin xamarin.forms xamarin.android

我正在尝试在我的xamarin表单应用程序中集成谷歌地图。我在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:App1"
                 xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
                 x:Class="App1.MainPage">
        <StackLayout VerticalOptions="StartAndExpand" Padding="30">

            <maps:Map WidthRequest="960" HeightRequest="700" 

           x:Name="MyMap" 

           IsShowingUser="True" 

           MapType="Street" 

           />
    </StackLayout>

</ContentPage>

以及我在MainPage.xaml.cs上的代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
namespace App1
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            MyMap.MoveToRegion(

           MapSpan.FromCenterAndRadius(

           new Position(37, -122), Distance.FromMiles(1)));
        }


    }
}

并在android应用程序下的Mainactivity.cs文件中执行以下代码:

 using System;

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

namespace App1.Droid
{
    [Activity(Label = "App1", Icon = "@drawable/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);
            LoadApplication(new App());
        }
    }
}

当我编译我的代码时,它编译成功。整个解决方案成功编译,并且单个android项目编译成功,但是当我运行我的代码时,我收到错误说

The name 'InitializeComponent' does not exist in the current context on MainPage.xaml.cs

The name 'MyMap' does not exist in the current context  on MainPage.xaml.cs

我尝试按Ctrl-S

保存我的MainPage.xaml页面

我真的需要帮助。任何帮助将受到高度赞赏。

谢谢。

0 个答案:

没有答案