8
votes

I am currently having a problem building my Xamarin.Forms solution. I'm getting an error of The "XamlCTask" task failed unexpectedly. I tried looking at the Xamarin logs and Stacktrace but I can't seem to find what's the issue. Any suggestions where I can start looking?

Logs

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>0</EventID>
<Type>3</Type>
<SubType Name="Warning">0</SubType>
<Level>4</Level>
<TimeCreated SystemTime="2016-10-11T10:45:07.4999078Z" />
<Source Name="Xamarin.VisualStudio.Android.Designer.MonoAndroidDesignerService" />
<Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" />
<Execution ProcessName="devenv" ProcessID="19260" ThreadID="1" />
<Channel />
<Computer>XXXXXX</Computer>
</System>
<ApplicationData>An unexpected error occurred trying to initialize Android Designer.</ApplicationData>
</E2ETraceEvent>

Stack Trace

Severity Code Description Project File Line Suppression State Error The "XamlCTask" task failed unexpectedly. System.ArgumentException: An item with the same key has already been added.

Server stack trace: at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at Xamarin.Forms.Xaml.XamlParser.ParseXamlElementFor(IElementNode node, XmlReader reader) at Xamarin.Forms.Xaml.XamlParser.ReadNode(XmlReader reader, Boolean nested) at Xamarin.Forms.Xaml.XamlParser.ParseXamlElementFor(IElementNode node, XmlReader reader) at Xamarin.Forms.Xaml.XamlParser.ReadNode(XmlReader reader, Boolean nested) at Xamarin.Forms.Xaml.XamlParser.ParseXamlElementFor(IElementNode node, XmlReader reader) at Xamarin.Forms.Build.Tasks.XamlCTask.ParseXaml(Stream stream, TypeReference typeReference) at Xamarin.Forms.Build.Tasks.XamlCTask.Compile() at Xamarin.Forms.Build.Tasks.XamlCTask.Execute() at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.Build.Framework.ITask.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext() XXXXXXX.XXXXXX.XXXXXX

11
It seems to have to do with objects having the same name. Check your recent changes carefully and check if they have the same x:Name property - Gerald Versluis
Unfortunately i have a lot of changes as I renamed all namespaces in the whole app but I don't remember adding x:Name in any of the components i touched. - Bon Macalindong
That sucks. Maybe some too much copy/pasting as well? Check the name properties for doubles, it's probably that - Gerald Versluis
Okay will take a look. - Bon Macalindong
I actually tried, on a working solution and added an element with the same x:Name, and the solution actually build. - Bon Macalindong

11 Answers

6
votes

tl;dr:

disable XamlC on the failing Page

[XamlCompilationAttribute (XamlCompilationOptions.Skip)]
public partial class MyPageThrowing {}

or at the Assembly level

[assembly:XamlCompilationAttribute (XamlCompilationOptions.Skip)]

Long Story

An issue throwing the same exception and the same StackTrace had been fixed in the next (to date) version of which should be 2.3.3-pre3 or 2.3.4.

The only way to know for sure would be to paste your failing Xaml page here, or even better, on http://bugzilla.xamarin.com.

I really encourage you to do so. If the issue is not fixed already. Either it's an issue in your Xaml and this needs a better exception being thrown, or it's an unsupported case in XamlC, and this require a fix.

1
votes

First of all this issue will be raised when xamarin.forms version is not compatible with the nuget that you are pointed to. So i'd like to tell you these steps and go ahead.

Step1: Update Xamarin.Forms version in PCL and all other projects.

Step2: Check for matching dependencies with nuget package.i.e make sure everywhere the the solution is using the **same** version of the Xamarin.Forms.

Hope this will helps you.

1
votes

Hope could be usefull to someone else, I had the same error shown in the question (..."XamlCTask" task failed unexpectedly. System.ArgumentException: An item with the same key has already been added...), my issue was caused by a ListViewTemplate, which contained a DataTemplate, in which I putted two elements, a Label and a Picker.

Grouping Label e Picker in a unique element, a Frame, inside the DataTemplate, solved my issue.

0
votes

I already resolved the issue. Since I was renaming the namespaces, i named the namespaces alias wrong. I used dots (.) in the alias name like this xmlns:sample.pages="clr-namespace:JustAnotherNamespace". When I removed the dots, the solution was able to build!

0
votes

I had the same problem. Clean or restart didn't help. Now I tried the simulator instead of the device. This worked. Afterwards I was able to build and run on the device.

Edit:

Now I had the issue a second time. But here it was different. The app didn't compile anymore. Now it seems to work. This is what I've done:

  • Upgrade to XF 2.3.3.168
  • Upgrade to XF 2.3.3.168 for the referenced project
  • Fixed a compile error (only appeared after update)
0
votes

This occurs sometimes if your Xamarin Forms nugets are going corrupt. Uninstall and reinstall them.

0
votes

Perhaps you have 2 instances of Visual Studio accessing the same device.

0
votes

in this case removing bin and obj folder it seems works like a charm!

0
votes

I had this error too. In my case it turned out to be a xaml issue. I hadn't made any recent changes to any dlls or references. I had just made some recent xaml changes and I suddenly started getting this error. This xaml I wrote had caused the issue:

            <StackLayout Grid.Row="4" Orientation="Horizontal" HorizontalOptions="Center" >
            <StackLayout.Children>
                <Label Text="Holes" BackgroundColor="White" VerticalOptions="Center"/>
                <Entry x:Name="entryHoles" BackgroundColor="White" WidthRequest="100"/>
            </StackLayout.Children>
            <StackLayout.Children>
                <Label Text="Par" BackgroundColor="White" VerticalOptions="Center"/>
                <Entry x:Name="entryPar" BackgroundColor="White" WidthRequest="100"/>
            </StackLayout.Children>
        </StackLayout>

There should be only one StackLayout.Children tag. This is what I switched to to get this error to go away:

            <StackLayout Grid.Row="4" Orientation="Horizontal" HorizontalOptions="Center" >
            <StackLayout.Children>
                <Label Text="Holes" BackgroundColor="White" VerticalOptions="Center"/>
                <Entry x:Name="entryHoles" BackgroundColor="White" WidthRequest="100"/>
                <Label Text="Par" BackgroundColor="White" VerticalOptions="Center"/>
                <Entry x:Name="entryPar" BackgroundColor="White" WidthRequest="100"/>
            </StackLayout.Children>
        </StackLayout>

I'll bet the extra StackLayout.Children tag caused this message: "An item with the same key has already been added." If so, it would have been great if Microsoft could have told us exactly what the duplicate item was. I wasted all kinds of time updating NuGet packages.

0
votes

This error indicates that the versions of the NuGet packages and the version of Xamarin.Forms do not match. Just update the NuGet packages in the project or remove Xamarin.Forms from the package list, then add it again.

0
votes

I had 35 pages in my app with an assembly reference to turn on/off xaml compilation. To find the offending page I had to remove the assembly reference and place the following on each page and rebuild the shared project one at a time until I found the offending page (make sure you have release build selected).

#if (DEBUG != true)
    [XamlCompilation(XamlCompilationOptions.Compile)]
#endif

Once you find the page, it's most likely a xaml issue like so many solutions above. For me it was because I had accidentally placed 2 resource dictionaries on the page so it was easy to spot, your's may not be so easy.