Search code examples
c#xamlvisual-studio-2015xamarin.formstabbed

Xamarin Tabbed Page error Error "The given key was not present"


I created a new blank Xamarin PCL app in Visual Studio 2015. It runs on all the platforms. I then add a XAML page named GuyBarSceneTabs to my PCL and change my app.cs file in my PCL so the App() code only contains

MainPage = new GuyBarSceneTabs();

This all works and I can run the projects again and it shows my XAML page. Next I add another XAML page name NearbyLocations

<?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="GuyBarScene.NearbyLocations"
             Title= "Nearby Locations">

</ContentPage>

I also then change the GuyBarSceneTabs page to be a tabbed page as follows and also change the code behind page to inherit from TabbedPage:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:GuyBarScene;assembly=GuyBarScene"
            x:Class="GuyBarScene.GuyBarSceneTabs">    
    <TabbedPage.Children>        
        <local:NearbyLocations  />         
    </TabbedPage.Children>
</TabbedPage>       
        
</TabbedPage.Children>

public partial class GuyBarSceneTabs : TabbedPage
{
    public GuyBarSceneTabs()
    {
        InitializeComponent();
    }
}

Now when I try to run the app I get the error message: "The given key was not present in the dictionary. There error is generated in the following code which is called from the InitializeComponent method of GuyBarSceneTabs page:

public partial class GuyBarSceneTabs : global::Xamarin.Forms.TabbedPage {

    [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
    private void InitializeComponent() {
        this.LoadFromXaml(typeof(GuyBarSceneTabs));
    }
}

Any suggestions as to what I am doing wrong? Do I need to change my App() code in some manner?


Solution

  • So I figured this out. This problem appears to manifest if you rename a XAML and corresponding cs file in your project. It appears something gets messed up in the project file. The easiest thing to do is add a new XAML page with a new name and copy things from the old XAML file to the new file. Then it appears to work. In my case, the problem was caused by renaming the XAML page the inherited from TabbedPage file. Hope this helps anyone else who has this problem.

    FYI the problem can also manifest itself if you include a XAML page and cs file from an other project and them rename them. In this case, when the project file gets messed up you will get an error that the InitializeComponent method does not exist will be thrown.

    I have to say I REALLY want Xamarin to work well. I hope now that it is owned by MS it becomes more of a reliable, first tier development solution.