Solving the “type or namespace name ‘Init’ does not exist” issue in Xamarin Forms

.NET, Technical stuff, Work, Xamarin
See comments

I don’t know about you but when I write cross-platform applications, I like to make sure that I immediately know which project is running on each platform. Over the years, I took the habit of suffixing the projects, for example as follows:

  • MyApp.Wp8 –> Windows Phone 8.
  • MyApp.Wpf –> Windows Presentation Foundation.
  • MyApp.Win8 –> Windows Store.

On Xamarin, it’s only natural to want to use the same concept:

  • MyApp.Droid –> Xamarin Android.
  • MyApp.iOS –> Xamarin iOS.
  • MyApp.Forms –> Xamarin Forms.

So far so good, right? Well almost, because it can cause an issue with the installed project template for Xamarin Forms.

Where is Init??

Follow the steps:

  • Create a new Xamarin Forms application and name it MyApp.Forms.
  • Try to build the application.
  • Look at your screen in confusion as the following error prevents the build from completing:

Error    1    The type or namespace name ‘Init’ does not exist in the namespace ‘MyApp.Forms’ (are you missing an assembly reference?)    C:\temp\MyApp.Forms\MyApp.Forms\MyApp.Forms.WinPhone\MainPage.xaml.cs    22    19    MyApp.Forms.WinPhone

So what’s up with that error? I didn’t even write any code!

Damn namespace

Well after a few confused moments, I reached out to the always helpful Eric (@ermau) and after a few messages he realized the issue: My unfortunate usage of the “Forms” suffix is it!

First we need to understand how Xamarin Forms works: When you create a new such application, you get a shared project (either a portable class library, or a Shared project like we have in Microsoft’s Universal application). Then you get one app per supported native framework. At the moment, this is iOS, Android and Windows Phone 8.0. Each native app is referencing the shared project as well as the Xamarin Forms framework. When the app starts, it “hooks” into the Forms framework and uses it to show the shared UI.

In Windows Phone, the “hooking” is done by a call to a static method called Init, which is located in the Forms class of the Xamarin.Forms namespace. Yes that it a lot of Forms, and it is a little unfortunate that the Xamarin team (who do an outstanding job with their cross platform solution!) chose this naming.

This is the MainPage constructor call, as created by the Xamarin Forms project template:

namespace MyApp.Forms.WinPhone
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();

            Forms.Init();
            Content = MyApp.Forms.App.GetMainPage().ConvertPageToUIElement(this);
        }
    }
}

In the current situation, the compiler is looking for the Init method in the MyApp.Forms namespace I just created. But there is no such method! Instead, we should use the fully qualified call to the Xamarin.Forms.Forms.Init() method for clarity. This immediately solves the issue:

public MainPage()
{
    InitializeComponent();

    Xamarin.Forms.Forms.Init();
    Content = App6.Forms.App.GetMainPage().ConvertPageToUIElement(this);
}

Good thing to keep in mind!

Happy coding
Laurent

Previous entry | Next blog entry

Responses to “Solving the “type or namespace name ‘Init’ does not exist” issue in Xamarin Forms”

  1. Adam Says:

    This blog post will be on the Xamarin Weekly Newsletter. Issue #5 will be out Friday September 12th.

  2. Solving the “type or namespace name ‘Init’ does not exist” issue in Xamarin Forms Says:

    […] , windows-store , xamarin , laurent-bugnion Read original post by Laurent Bugnion at GalaSoft BlogI don’t know about you but when I write cross-platform applications, I like to make sure that […]

  3. Andreas Hammar Says:

    Thanks for the tip! So natural to call the project “Forms” – I did that too :)

Comments for Solving the “type or namespace name ‘Init’ does not exist” issue in Xamarin Forms