Error adding MasterDeail Page “The namespace ‘SampleMasterPage’ already contains a definition for ‘MasterPage’ ” in Xamarin Forms – How to Fix it ?

Video

Objective => 

When adding a “Master Detail” Page to the PCL(Portable Class Library) in Xamarin Forms, It gives an error which says the MasterPage is already exists in the same namespace. And also we can see few compilation errors too.

3 error

Image 1 : Errors

The namespace ‘SampleMasterPage’ already contains a definition for ‘ProductMasterPage’

 

Reason for the error…

Then I tried to find the reason for the error and I  found the reason for the error is the XML Namespace declared in the XAML(UI) file is not equal to the namespace of the code behind class of the XAML(UI) file.

So How to Fix it ?

To fix the error, we have to change the namespace in the code behind class of the XML file same as the XML namespace declaration  in the XAML file.

You can follow below Video to fix the issue…

 

Here are the Steps to Fix the Error..

1. Right Click on PCL project -> Add -> Add New Item -> Select MasterDtail Page -> Give a Name “ProductMasterPage” – > Click Add

2. Then you can see four new files has been added to the project

2 files beongs to master page

Image 2 : Master Page and Related classes

The “ProductMasterPage” is the Page which contains the Side menu and the Main Content. You can see in the below image, “ProductMasterPageMaster” and “ProductMasterPageDetail” has referenced in ProductMasterPage. The ProductMasterPageMenuItem class is a Model using to declare Side menu items.

1 main master page

Image 3 : ProductMasterPage and referenced pages

In the header of Product Master page we can see the xml namspace definition…


xmlns:pages="clr-namespace:SampleMasterPage;assembly=SampleMasterPage"

3. Then Rebuild the project

When trying to rebuild the project, the app will not compile successfully and we will see some errors..

3 error

Image 1 : Errors

The namespace ‘SampleMasterPage’ already contains a definition for ‘ProductMasterPage’

The name ‘MasterPage’ does not exist in the current context

The name ‘ProductMasterPageMenuItems’ does not exist in the current context

The name ‘ListViewMenuItems’ does not exist in the current context

4. Lets go to the code behind of the code behind of all MasterPage related classes…

The you can see the namespace of these classes as “SampleMasterPage.ProductMasterPage” and it is not the names[ace and assembly name declared in XAML namespace definition.

4 namespace is different in class

Image 4 : ProductMasterPageMaster.xaml.cs

5 namespace is different in productmp

Image 5 : ProductmasterPage.xaml.cs

4 namespace is different in model class

Image 6 : ProductMasterPageMenuItem.cs   (Model)

5. So to fix the error , what we have to do is change the namespace in these class to the same namespace which is declared in the XAML namespace definition.

When you change the namespace, you can see some errors will be disappear..

6 namespace changes

Image 7 : Change namespace in ProductMasterPageMaster.xaml.cs

6. The rebuild again the android project..

And you will see another error says..

“ProductMasterPageMenuItems does not exists in the current context.”

 

7 product menu items

Image 8 : Rebuild Error

“ProductMasterPageMenuItems” is the List of <ProductMasterPageMenuItem>. which is a Observable Collection.

But there is no definition for the List. But you can see above there is a definition for a List of “ProductMasterPageMenuItem” which is “MenuItems”. And it is the name of the MVVM binding of the ListView of “ListViewMenuItems”

8 mvvm binding

Image 9 : ListView items Binding using MVVM

7. So we have to change the “ProductMasterPageMenuItems” to “MenuItems”.

9 change menu item name

Image 10 : Change List Name according to Binding

8. Then Re-build the project and it will be compile successfully.

Thanks !