Xamarin System.Exception when adding Content page

2020-06-03 08:04发布

问题:

When trying to add a Content Page to the solution in visual studio, the following error occurs:

Exception of type 'System.Exception' was thrown.

The project system has encountered an error.

Did not find new element in the hierarchy for item `Views\MyPage.xaml`.
The item would have been added with type "Page".
The project item schema service knows about this item type.
The item does exist in the project file.

When adding the file I do the following:

Right click on Views in my Xamarin Solution > Add > New Item > Content Page > Add

When looking in the windows explorer, there is a file created MyPage.xaml but it has not created MyPage.xaml.cs and it is not added to the solution

It is a blank Xamarin shared project and this error just keeps happening...

It's driving me nuts as I can't even add a single page

As a workaround, I add the files manually using the W. explorer and then edit app.projitems manually, but this is just ridiculous.

回答1:

As a dirty workaround until this gets fixed, adding the files "manually" to the project gets the job done.

With Visual Studio closed,

  1. copy/paste existing (working) *.xaml and *.xaml.cs files (rename them as you please)
  2. open the newly pasted files with a text editor (not VS) and rename the contents to your chosen name in #1
  3. add newly pasted files to your *.projitems file (open it in a text editor, search for the filenames you copied in #1 and add the new ones similarly) - there'll be 3 places to add them

Now open the solution in VS and rebuild - it should look and build fine.



回答2:

Apparently, if you go into the VS2017 installer and install the Universal Windows Platform (UWP) development workload, this fixes the issue.



回答3:

Make sure you have StackLayout instead of ContentPage.Content in your .xaml files that throw this exception on VS.