Bug 55921 - Cannot Use OnPlatform for Color Resource in Xaml
Summary: Cannot Use OnPlatform for Color Resource in Xaml
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.5
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2017-05-03 16:01 UTC by Solvum Developer
Modified: 2017-05-29 12:48 UTC (History)
3 users (show)

Tags: xaml onplatform color
Is this bug a regression?: Yes
Last known good build: 2.3.4


Attachments
repro project (51.30 KB, application/zip)
2017-05-18 18:13 UTC, Jimmy [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Solvum Developer 2017-05-03 16:01:46 UTC
I'm using App.xaml in my project, to define Colors among other things.

Previously in a fashion like <Color x:Key="PrimaryColor">#DD424C</Color>

After updating from 2.3.3.193 to 2.3.5.235-pre2

i got this:

Xamarin.Forms.Xaml.XamlParseExceptionPosition 34:6. Can not set the content of Color as it doesn't have a ContentPropertyAttribute
Raw
Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit(ElementNode node, INode parentNode)
Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
Xamarin.Forms.Xaml.FillResourceDictionariesVisitor.Visit(ElementNode node, INode parentNode)
Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
Xamarin.Forms.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
Xamarin.Forms.Xaml.XamlLoader.Visit(RootNode rootnode, HydratationContext visitorContext)
Xamarin.Forms.Xaml.XamlLoader.Load(object view, string xaml)
Xamarin.Forms.Xaml.XamlLoader.Load(object view, Type callingType)
Xamarin.Forms.Xaml.Extensions.LoadFromXaml<TXaml>(TXaml view, Type callingType)<4b9ee1a351044745a3b4ec4e308986d0>:0
STM.App.InitializeComponent()
STM.App.SendOnAppLinkRequestReceived(Uri uri)

I figured "oh well. i guess they removed the ContentProperty attribute. I'll just specify it in the property directly."

So i changed it to this:

<Color x:Key="PrimaryColor" Accent="#DD424C"></Color>

Unfortunately, that only changed the exception message.

Xamarin.Forms.Xaml.XamlParseExceptionPosition 21:32. Cannot assign property "Accent": Property does not exists, or is not assignable, or mismatching type between value and property
Raw
Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue(object xamlelement, XmlName propertyName, object value, object rootElement, INode node, HydratationContext context, IXmlLineInfo lineInfo)
Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit(ValueNode node, INode parentNode)
Xamarin.Forms.Xaml.ValueNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
Xamarin.Forms.Xaml.FillResourceDictionariesVisitor.Visit(ElementNode node, INode parentNode)
Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
Xamarin.Forms.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
Xamarin.Forms.Xaml.XamlLoader.Visit(RootNode rootnode, HydratationContext visitorContext)
Xamarin.Forms.Xaml.XamlLoader.Load(object view, string xaml)
Xamarin.Forms.Xaml.XamlLoader.Load(object view, Type callingType)
Xamarin.Forms.Xaml.Extensions.LoadFromXaml<TXaml>(TXaml view, Type callingType)<4b9ee1a351044745a3b4ec4e308986d0>:0
STM.App.InitializeComponent()
STM.App.SendOnAppLinkRequestReceived(Uri uri)

I would try to reproduce in 2.3.4 "stable" but updating to that gave me a generic NRE upon launch, which is why i am trying it in the preview version.
Comment 1 Jimmy [MSFT] 2017-05-03 17:53:10 UTC
Thank you for filing this report! I tested with a new sample project, but I was not able to reproduce the issue with Xamarin.Forms 2.3.5-pre2. 

What platform are you experiencing this on? Can you attach a minimal reproduction project so we can look into this further?  When providing the requested information please set the report's status back to NEW, otherwise we may close this issue after 30 days of no response. Thanks!
Comment 2 Solvum Developer 2017-05-04 12:49:45 UTC
Hello Jimmy,

I've experienced this issue on an iPad i'm using for development.
I attempted to set up a minimal reproduction solution for you. I attempted to set up a reproduction project for you to look into this - However i am unable to reproduce the issue outside of my production app, despite double checking the package references.

Would you be willing to have a look at it on my machine using Teamviewer or something similar? Maybe it's some specific setting in the project i'm overlooking here.
Comment 3 Solvum Developer 2017-05-05 09:12:58 UTC
Hello Jimmy,

I have another suggestion: Could you please set up a tutorial video on how to get original xamarin sources, to compile the dll's and reference these into a production solution to reproduce an issue and potentially fix an issue yourself?

I have downloaded the sources myself before and began with a branch to fix this, but in a unit testing scenario i was unable to reproduce the described behaviour. So referencing self compiled sources for a full debugging experience may be the only way to figure out what exactly is going wrong in my case.
Comment 4 Jimmy [MSFT] 2017-05-05 21:26:35 UTC
Hi Andreas, 

Unfortunately I wouldn't be able to do a TeamViewer session, but there are a couple of the other things we can try right now.

Since you are not able to reproduce this in a new project, that does indicate that the issue may be with your project. You mention that the crash seems to come from the <Color> element in the App.xaml, so can you test if doing

> MainPage = new ContentPage();

still causes the crash? This would help confirm that the issue is within App.xaml. At the same time, I noticed that the stack trace indicates the app crashed after being launched via an AppLink. Does it also crash when it is launched normally?

Can you also enable XamlC[1] to see if it catches any errors in the xaml during compilation? It will also be interesting to see if the crash still occurs after this is enabled.

Finally, regarding how to build the Xamarin.Forms source, I don't have a video tutorial, but I do have a basic outline that lists what dlls you need for each platform and which projects in the solution to build that might help. You can check it out here: https://gist.github.com/jimmgarrido/5f5b4b045464ad98aeae42638a5b7d45

[1] https://developer.xamarin.com/guides/xamarin-forms/xaml/xamlc/
Comment 5 Solvum Developer 2017-05-08 13:40:19 UTC
public App()
		{
			try
			{
				InitializeComponent();
				MainPage = new ContentPage() {BackgroundColor = Color.Orange};
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.StackTrace);
				AnalyticsWrapper.Report(ex, AnalyticsWrapper.Severity.Critical);
			}
		}

Unfortunately even with your suggestion the application crashes with the same exception.

Using [assembly: XamlCompilation(XamlCompilationOptions.Compile)] I only get one compilation error - the same one. "Can not set the content of Color as it doesn't have a ContentPropertyAttribute"

If that helps narrowing down the issue: my shared project is a pcl netstandard 1.5 project using   "frameworks": {
    "netstandard1.5": {
      "imports": "portable-net45+wpa81+wp8+win8"
    }
  }
Comment 6 Solvum Developer 2017-05-16 14:40:28 UTC
Are you able to reproduce the issue now?
Comment 7 Jimmy [MSFT] 2017-05-18 18:13:14 UTC
Created attachment 22283 [details]
repro project

This appears to be an issue when using OnPlatform for a Color resource in Xaml. This happens with both the old and new OnPlatform syntax. The app will crash (or fail to build with XamlC enabled) with 

> Can not set the content of Color as it doesn't have a ContentPropertyAttribute

Full stack trace: https://gist.github.com/jimmgarrido/0bf97b7cb8a8d7b03bde675af50702f7

I am attaching a sample project that reproduces the issue. This issue does not occur with 2.3.4 so this is a new regression.


### Regression Test
2.3.5-pre3  BAD
2.3.5-pre1  BAD
2.3.4.247   GOOD
Comment 10 Stephane Delcroix 2017-05-24 09:22:07 UTC
I'm not sure the repro in comment #c7 is valid for this bug. Could you please attach the simplest Xaml file triggering the issue.

Thanks a lot,

The XF team
Comment 11 Solvum Developer 2017-05-24 09:31:20 UTC
Hello Stephane. In reference to #9

(example 1)
<Color x:Key="ControlBackgroundColor">
				<OnPlatform x:TypeArguments="Color" Android="Transparent" WinPhone="White" iOS="White" />
			</Color>

is the style which i used in <2.3.4.

2.3.4 marks WinPhone etc as deprecated or obsolete. Changing it to the wrapped OnPlatform in Color with nested "On" expressions was the way i expected it to work furthermore as well - since example 1 worked similarly.

I checked Jimmys repro sample and was also able to reproduce the crash too.
Comment 12 Solvum Developer 2017-05-24 09:42:03 UTC
Correction. i cited the wrong version numbers:

is the style which i used in <2.3.5.

2.3.5  ...
Comment 13 Stephane Delcroix 2017-05-24 11:02:05 UTC
I re-tested. that syntax never worked on 2.3.3 or 2.3.4. If you confirm that the same issue you got, I can close this.
Comment 14 Solvum Developer 2017-05-24 12:16:21 UTC
Am i understanding you correctly that:

<Color x:Key="MyColor">
  <OnPlatform x:TypeArguments="Color">
    <On Platform="iOS" Value="Pink" />
    <On Platform="Android" Value="Red" />
  </OnPlatform>
</Color>  

will be fixed to work without a crash in 2.3.5 onwards then?

As it is similar to the version of the working <= 2.3.4

<Color x:Key="ControlBackgroundColor">
    <OnPlatform x:TypeArguments="Color" 
       Android="Transparent" 
       WinPhone="White" 
       iOS="White" />
</Color>

syntax?
Comment 15 Stephane Delcroix 2017-05-24 18:28:27 UTC
no. the second syntax wasn't crashing, but it wasn't working.

we have no plan to make that invalid syntax works. `<Color>xxx</Color> only supports strings literals.

the new behavior, which is runtime or compilation crash, is a better one than the previous one (silently ignoring)
Comment 16 Solvum Developer 2017-05-29 12:48:59 UTC
If this never worked to begin with and this crash is now merely a result of improved xaml compilation feedback that's fine i guess. Thanks for the clarifaction then.

Also yes - crash > silently failing.

Thanks Stephane.