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 !

Advertisements

‘Xamarin.Android.Support.Design 25.3.1’ is not compatible with ‘Xamarin.Forms 2.3.4.231 constraint: Xamarin.Android.Support.Design (= 23.3.0)’. – How to fix it ?

Why ?

When I was trying to update the NuGet Packages for one of my Xamarin projects, I got an error saying “Unable to resolve dependencies.” and the packages wasn’t updated.

The detailed error message is …

Unable to resolve dependencies. ‘Xamarin.Android.Support.Design 25.3.1’ is not compatible with ‘Xamarin.Forms 2.3.4.231 constraint: Xamarin.Android.Support.Design (= 23.3.0)’.

1 error

Image 1 : Error

The reason for the error was…

The Android project of my Xamarin solution has been configured to Compile using Android 6.0(Marshmallow) API 23.3.0 . But I was trying to update the Android packages for Android 7.1 (Nougat) API 25.3.1.

3 current versions

Image 2 : Current Android application properties(Compile using Android 6.0)

So the target compilation version is not compatible with the updating version of Android packages.

How To Fix the Error ?

To fix the error we have to change the target framework for compile to “Android 7.1(Nougat)”.

Here are Steps to change the target compile framework..

2. go to properties

Image 3 : Right Click on Android Project -> Properties

3 current versions

Image 4 : Current Android application properties(Compile using Android 6.0)

4 select api nouget

Image 5 : Change Compile Android Version to Android 7.1 (Nougat)

When Change the target compilation version of Android , Visual Studio will ask you to restart the project to configure itself…

5 restart project

Image 6 : “Yes” to close and reopen

6 lading

Image 7 : App is loading

7 go to nuget

Image 8 : Again go to NuGet package manager to update Android packages

8 download

Image 9 : Select Android Packages v.25.3.1(or the package showing here)

9 accept

Image 10 : Accept to Agree licence and tearms

9 conform update

Image 11 : You can see changes will be happening (the old packages and new packages). Click OK.

10. installing packages

Image 12 : Then the packages will be downloaded and Install Successfully.

Then Re-Build the project and the app will run successfully…

Thanks !

Xamarin Lesson 2 : Create a Cross-Platform Mobile Project in Visual Studio 2017

Video

This video demonstrates, How to create cross-platform mobile app Including Android/iOS/Windows Phone Native apps and a Portable Class Library(PCL) in Visual Studio 2017

Thanks !

Xamarin Lesson 1 – Install Xamarin with VS 2017

Video

This Video show you how to download and install Visual Studio for the development of Cross-platform mobile apps.

Please Follow the video.. To Download and Install Visual Studio 2017…