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 !

Fix Error : “Could not find a part of the path” While Publishing ASP.NET Core MVC Application To the Target Platform of Windows 7 x64/x86

Reason :

I’m working on a ASP.Net Core MVC application and I Published one of my ASP.Net Core MVC app into a Server which is running Windows Server 2008 R2.

But when I released the application live, it didn’t work on the server but it works fine on my local machine. My Local machine has Windows 10.

Then I looked for a solution and I got the reason. The reason Is I have published the app for the target platform of Windows 10. But the Server that the site has hosted has Windows Server 2008 R2 (Windows 7 Configurations)

Then what I had to do I was : Publish the app for the target platform of windows 7 (Windows Server 2008 R2).

Then I added target platforms to to the project.json file in my .Net Core MVC app. I published it again.

But I wasn’t published and it gave me an error..

Could not find a part of the path ‘F:\Projects\TestWebCore\src\TestWebCore\bin\Release\netcoreapp1.0\win7-x64\TestWebCore.dll’.

Then I looked for a solution and I found it is an error in tooling.

http://stackoverflow.com/questions/39023224/error-could-not-find-a-part-of-the-path-while-publishing-net-core-application

Solution :

The Solution is we have to publish the .Net Core applicaion using command prompt. 

I did it and my app was published successfully.

 

Here I’ll show you how the error occurred and How I did fix it.

First I’ll create a sample .Net Core MVC app with a .Net Core Portable Class Library(PCL).

You can See by Clicking..

How to Create a ASP.Net Core MVC app

Here is the project I have created..

The solution consists of one ASP.Net Core MVC project and a .Net Core PCL project.

1-project-flow

Image 1 : Solution Structure

Then I have changed the Index.cshtml file and added a simple html content to show you ..

2-change-index-page

Image 2 : Sample HTML content in Index.html file

Lets run the app to see how it looks like.

The app will run with the localhost and you could see the content I have added to the html body. I have used a Bootstrap template to the project. So you can see it is responsive.

3-index-page

Image 3 : Run The app in localhost

So Its running well and and the next step is Publish the app.

Publish The app

Then you try to publish an app for the first time, you will see a window like below..

5-publish-without-target-framework

Image 4 : Default Publish Window

Here you can see only one menu which is “Profile”. You can’t publish the app without selecting a profile. So we have to create a publish profile.

for that click on “Custom” button and you will see a dialague box to enter the publish profile name.

6-create-a-publish-profile

Image 5 : Create a Publish Profile

I’ll create it as “MyPublishProfile”. And click “OK”. Then the profile will be created and you will see new menus in the left hand side now.

7-selet-profile-menu

Image 6 : New menus will appear when Publish Profile created.

Then go to connection menu and select a folder to publish the web application. And Select the publish method as “File system”.

9-select-a-publish-location

Image 7 : Connection menu(Select Target Publish Folder)

Then go to the settings menu and you can select the target frameworks to publish the app.

8-target-runtme-any-settings

Image 8 : Default Settings Menu.

But according to above image, you can’t see a Target Runtime. 

By default it has set to “Any“. 

So I’m adding some target runtimes to project to show how the error is happening.

We have to add those settings to the project.json file in the web application project.

To see How to add Target Runtimes to project.json  Click Here…

If you have already added target  Runtimes, Now we are ready to publish the app.

So Right Click and Select Publish in the context menu and the publish window will open.

Publish to the Target Runtime of Windows 10

Then Click “Settings” and Select “Win10-x64” from the drop down list.

5-select-target-platform-10

Image 9 : Select Windows 10 as Target Runtime

Then Click Preview menu and verify is the details correct before publish your app.

6-conform-details-before-publish

Image 10 : Verify Publish information before publish the app

Then Click “Publish” and the application will publish to the selected folder. You will see publish details output window.

7-success-win-10

Image 11 : App has publish successfully for the Target Runtime of  Windows 10 x64

Then check the selected publish folder and you will see compiled files has published to the folder.

The “TestWebCore” app has publish to the target runtime of “Windows 10 64bit” successfully.

How The Error Occurred…

Publish to the Target Runtime of Windows 7

This is same as we published the app for Windows 10. But the only difference is we have to change the target runtime to “Win7-x64

11-publishing-to-win-764

Image 12 : Select Windows 7 x64 as Target Runtime

Then Click “Publish”.

When I did it, I got below error.

Could not find a part of the path ‘F:\Projects\TestWebCore\src\TestWebCore\bin\Release\netcoreapp1.0\win7-x64\TestWebCore.dll’.

12-error

Image 13 : Error Publishing app to Windows 7

How to Fix the Error

To publish without an error, what I did is ..

Run the Publish command in Command Prompt.

So Here is the way I did ..

13-publish-command

Image 14 : Run Publish command in Command Line

When you press “Enter” when type the publish command, you will see the application publish successfully.

14-published-to-win7-64

Image 15 : Successfully Published to Windows 7 x64

You can see the published files in the publish folder.

This Error is not happening

References…

To Learn More about Target Runtimes/Frameworks Click…

Publish Commands for .Net Core in Command Line Click…

How to Publish Self Contained Application…

Thanks !

Create a File/Directory Transferring App in C#

Reason :

One of a application I’m developing generates many PDF files daily. So we delete them normally once a week or another time. But it is difficult to search files which has generated in a particular date or date range and copy them or delete them to another place. We had to do it manually.

Solution:

SO I created a small app to do it quickly. And I created a small app to show you how to create a example app to copy and delete directories/files from one place to another.

Steps :

1 .Create User Interface.

Here is the user interface I created..

2 drop folder open dialague

Image 1 : UI

We have to select folder path to the app. So I added two folder browse dialogues to select browse folders. You can select “Folder browser dialogue” control form tool box and drag and drop it into above form.

1 folder open dialague

Image 2 : Folder browse control

Then you can see those dialogue controls in bottom panel.

2. We have to Add function to “Source” button and “Destination” button to browse and select source folder path and destination folder path.

3 select directories with delete option

Image 3 : Source and Destination selecting


private void btnSource_Click(object sender, EventArgs e)
 {
 DialogResult result = folderBrowserDialogSourceDir.ShowDialog();
 if (result == DialogResult.OK)
 {
 textBox1.Text = this.folderBrowserDialogSourceDir.SelectedPath;
 }

}

private void btnDestination_Click(object sender, EventArgs e)
 {
 DialogResult result = folderBrowserDialogDestinationDir.ShowDialog();
 if (result == DialogResult.OK)
 {
 textBox2.Text = this.folderBrowserDialogDestinationDir.SelectedPath;
 }
 }

Then when you click one of “Source” or “Destination” buttons, a browser window will popup and you can select any folder.

3 select directories

Image 4 : Folder browser dialague

3. User can check the “Delete after transferring”  check box if he want to delete files from source folder after copy them to destination.

4.Next Add functions to “Transfer files” button  to transfer files when Click it. So I use “Button click” event.

Here is the code in this event.


private void btnTransfer_Click(object sender, EventArgs e)
 {
 try
 {
 string pathDestinationFolder = string.Format(@"{0}", textBox2.Text);

string pathSource = string.Format(@"{0}", textBox1.Text);

if (!string.IsNullOrWhiteSpace(pathSource) && !string.IsNullOrWhiteSpace(pathDestinationFolder))
 {
 DateTime selectedDate = Convert.ToDateTime(dpEffiectiveDate.Text);
 DirectoryCopy(pathSource, pathDestinationFolder, true, selectedDate);

if (chkDelete.Checked)
 {
 DeleteDirectories(pathSource, selectedDate);
 }

lblDirectoriesFound.Text = foundDirectories.ToString();
 lblTransferedFilesCount.Text = createdDirectories.ToString();
 lblDeleted.Text = deletedDirectories.ToString();
 lblNotTransfered.Text = (foundDirectories - createdDirectories).ToString();
 }
 else { MessageBox.Show("Source and destination is required", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information); }
 }
 catch (Exception ex)
 {
 MessageBox.Show(MethodBase.GetCurrentMethod() + " -> " + ex.Message);
 }
 }

I have extracted the function to copy directories and files to destination to a method “DirectoryCopy”.


private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, DateTime selectedDate)
 {
 try
 {
 #region check exists and create is not exists
 // Get the sub directories for the specified directory.
 DirectoryInfo dir = new DirectoryInfo(sourceDirName);
 DirectoryInfo[] dirs = dir.GetDirectories();

if (!dir.Exists)
 {
 throw new DirectoryNotFoundException(
 "Source directory does not exist or could not be found: "
 + sourceDirName);
 }

// If the destination directory doesn't exist, create it.
 if (!Directory.Exists(destDirName))
 {
 Directory.CreateDirectory(destDirName);
 createdDirectories = createdDirectories + 1;
 dirList.Add(destDirName);
 listViewDirectories.Items.Add("Created Directory -> " + destDirName);
 }

// Get the files in the directory and copy them to the new location.
 FileInfo[] files = dir.GetFiles();
 foreach (FileInfo file in files)
 {
 string temppath = Path.Combine(destDirName, file.Name);
 if (File.Exists(temppath))
 { }
 else
 {
 file.CopyTo(temppath, false);
 listViewDirectories.Items.Add("Coppied file -> " + temppath);
 }
 }
 #endregion
 // If copying subdirectories, copy them and their contents to new location.
 if (copySubDirs)
 {
 foreach (DirectoryInfo subdir in dirs)
 {
 if (subdir.CreationTime.Date == selectedDate.Date)
 {
 foundDirectories = foundDirectories + 1;
 string temppath = Path.Combine(destDirName, subdir.Name);
 DirectoryCopy(subdir.FullName, temppath, copySubDirs, selectedDate);
 }
 }
 }
 }
 catch (Exception ex)
 {
 MessageBox.Show(MethodBase.GetCurrentMethod()+" -> "+ ex.Message);
 }

}

“DirectoryCopy” Method is a recursive function.

Recursive function is a method which calls itself.

for more information go to =>Recursive function

7 use recursive function

Image 5 : Recursive function

The reason is some directories has one or more sub directories. So we have to go through all of them. So we have to loop through all directories in a same war. For that we have to use a Recursive function.

In “DirectoryCopy” method we have to use “FileInfo” class and “Directory“. SO we have to import System.IO namespace.


using System.IO;

[/code</blockquote>
In this method first we are checking directories in the source directory and we can watch information of source directory when debugging it.
<pre>

DirectoryInfo dir = new DirectoryInfo(sourceDirName);

4 file info creation date

Image 6 : File info -> Quick watch

According to my requirement  I get folders and files by its creation date. So I have to use "FileInfo" class to get values.

Then we have to get sub directories in the folder. For that I use GetDirectories() method.


DirectoryInfo[] dirs = dir.GetDirectories();

when debugging the code we can check whether it has sub directories or not.

5 sub directories

Image 7 : Directory info

  • According to above "older is exists and if it is not existing, Then I'm reading a folder to copy files.
  • And then read File information of containing files of resource folder and if its date is my selected date, then copy those files to selected destination.
  • Then show copied files and created folders in the list-box.

After copy files, In  Transfer button click event, then I check the "chkDelete" check box and if it is checked, Then program delete folders from source folder.

DeleteDirectories() is a recursive function too.


private void DeleteDirectories(string pathSource, DateTime selectedDate)
 {
 try
 {
 // Get the subdirectories for the specified directory.
 DirectoryInfo dirs = new DirectoryInfo(pathSource);
 DirectoryInfo[] dirList2 = dirs.GetDirectories();
 foreach (DirectoryInfo subdir in dirList2)
 {
 string subDirname = subdir.FullName;
 if (subdir.CreationTime.Date == selectedDate.Date)
 {
 deletedDirectories = deletedDirectories + 1;
 Directory.Delete(subDirname, true);
 listViewDirectories.Items.Add("Deleted Directory ->" + subDirname);
 }
 else
 {
 DeleteDirectories(subDirname, selectedDate);
 }
 }
 }
 catch (Exception ex)
 {
 MessageBox.Show(MethodBase.GetCurrentMethod() + " -> " + ex.Message);
 }
 }

Then we can see copied files , created directories and deleted directories in the list box.

8 final info

Image 8 : Final Result

And another thing is if an error occurred, System will shows the error message with the Method name. To get the method name that I used ..


MethodBase.GetCurrentMethod()

to use the MethodBase class we have to import ...


using System.Reflection;

To download the code go to Download.

Thanks !

How to Fix Error : SQL Server blocked access to procedure ‘sys.sp_OACreate’ of component ‘Ole Automation Procedures’ because this component is turned off as part of the security configuration for this server.

Problem :

When I was executing a stored procedure in a SQL Server 2005 Database from my C# application, it gave me an error and here is the result I saw when I was debug it..

ole auto error 2

Image 1 : Exception Message

SQL Server blocked access to procedure ‘sys.sp_OACreate’ of component ‘Ole Automation Procedures’ because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of ‘Ole Automation Procedures’ by using sp_configure. For more information about enabling ‘Ole Automation Procedures’, see “Surface Area Configuration” in SQL Server Books Online.

And also I got below error messages with this error. I did not include all information of that message because it is too long..

SQL Server blocked access to procedure ‘sys.sp_OASetProperty’ of component ‘Ole Automation Procedures’ because this component is turned off as part of the security configuration for this server…
SQL Server blocked access to procedure ‘sys.sp_OAMethod’ of component ‘Ole Automation Procedures’ because this component is turned off as part of the security configuration for this server…
SQL Server blocked access to procedure ‘sys.sp_OADestroy’ of component ‘Ole Automation Procedures’ because this component is turned off as part of the security configuration for this server…

Then I searched solutions to fix this error and I found the solution.

Solution :

According to the message we have to enable the OLE Automation feature in Surface area configuration.

Here are steps to Enable the feature..

0 surface search

Image 2 : Search -> SQL Server Surface Area Configuration in Start menu

1 surface search

Image 3 : Select and click Surface Area Configuration for Features

Then you will enter to another form which contains features. Then Select “OLE Automation”

3 ole uttomation

Image 4 : Select OLE Automation

 

4 enable ole automation

Image 5 : Check the Enable OLE Automation Check box

Then Click “OK”, And Click “Apply”.

But when I click “OK”, I got another error that..

User does not have permission to perform this action.

You don’t have permission to RECONFIGURE statement.

5 enable error

Image 6 : Permission error

To fix this error, I had to give permission to the user..

6 set permissions

Image 7 : Give permission to the user

Then you can apply OLE Automation Feature..

Thanks !

Multiple substitutions specified in non-positional format; did you mean to add the formatted=”false” attribute?

Reason

My Xamarin project uses multiple languages and their translations are stored in Resource/.resx files. When I build the project in VS 2015 the android project not built successfully and it gave me an error…

Error

Multiple substitutions specified in non-positional format;
did you mean to add the formatted=”false” attribute?
Mobility_App.Droid F:\Mobility_App.Droid\Resources\Values-fr\AppResources.xml

1 error

Image 1 : Error

Reason

I search the error and I found the issue occurred because some <string> tags consists of percentage(%) symbol.

2 reason

Image 2 : Reason % symbol

Solution

To fix this error we have to add a attribute “Formatted” to <string> tags which are consists of ‘%’ symbols. and Set “Formatted” attribute’s value to “false”.

3 solution

Image 3 : add <string formatted=”false”>

Now the issue has fixed and to ensure, then rebuild the project and you can see the solution is success.

4 success

Image 4 : Successfully Build

Thanks you !