Bug 59818 - StaticResource binding in ColumnDefinition fails with type mismatch Exception
Summary: StaticResource binding in ColumnDefinition fails with type mismatch Exception
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: 2.4.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2017-09-28 12:31 UTC by david
Modified: 2017-10-20 17:53 UTC (History)
5 users (show)

See Also:
Tags: ac fr
Is this bug a regression?: Yes
Last known good build: 2.3.4


Attachments
Repro project (102.36 KB, application/zip)
2017-09-28 12:31 UTC, david
Details

Description david 2017-09-28 12:31:51 UTC
Created attachment 24990 [details]
Repro project

I have a simple form where I bind the <ColumnDefinition> of a Grid to a value in the Application's ResourceDictionary, like so:


<ColumnDefinition Width="{StaticResource NumberColumnWidth}" /> <!-- test -->



This binding worked fine in 2.3.4.237 and previous.

After upgrading to 2.4.0.280, the binding fails with a runtime exception as the form is being loaded:


Xamarin.Forms.Xaml.XamlParseException: Position 30:24. Cannot assign property "Width": Property does not exists, or is not assignable, or mismatching type between value and property
  at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue (System.Object xamlelement, Xamarin.Forms.Xaml.XmlName propertyName, System.Object value, System.Object rootElement, Xamarin.Forms.Xaml.INode node, Xamarin.Forms.Xaml.HydratationContext context, System.Xml.IXmlLineInfo lineInfo) [0x000ee] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:341
  at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.Forms.Xaml.INode parentNode) [0x00152] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:124
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x000ab] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:149
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00043] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:143
  at Xamarin.Forms.Xaml.ListNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00025] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:227
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00043] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:143
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00078] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:145
  at Xamarin.Forms.Xaml.RootNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00043] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:201
  at Xamarin.Forms.Xaml.XamlLoader.Visit (Xamarin.Forms.Xaml.RootNode rootnode, Xamarin.Forms.Xaml.HydratationContext visitorContext) [0x0007b] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:141
  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml) [0x0004b] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:89
  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType) [0x0002f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:68
  at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.Type callingType) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ViewExtensions.cs:36
  at ColumnWidthTest.ColumnWidthTestPage.InitializeComponent () [0x00001] in /Users/davidd/Documents/Projects/Programmation/Experimental/ColumnWidthTest/ColumnWidthTest/obj/Debug/ColumnWidthTest.ColumnWidthTestPage.xaml.g.cs:20
  at ColumnWidthTest.ColumnWidthTestPage..ctor () [0x00008] in /Users/davidd/Documents/Projects/Programmation/Experimental/ColumnWidthTest/ColumnWidthTest/ColumnWidthTestPage.xaml.cs:9
  at ColumnWidthTest.App..ctor () [0x0000f] in /Users/davidd/Documents/Projects/Programmation/Experimental/ColumnWidthTest/ColumnWidthTest/App.xaml.cs:11
  at ColumnWidthTest.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication app, Foundation.NSDictionary options) [0x0000d] in /Users/davidd/Documents/Projects/Programmation/Experimental/ColumnWidthTest/iOS/AppDelegate.cs:22
  at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/5481/9ddb5e6c/source/xamarin-macios/src/UIKit/UIApplication.cs:79
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/5481/9ddb5e6c/source/xamarin-macios/src/UIKit/UIApplication.cs:63
  at ColumnWidthTest.iOS.Application.Main (System.String[] args) [0x00001] in /Users/davidd/Documents/Projects/Programmation/Experimental/ColumnWidthTest/iOS/Main.cs:17



This is a regression and is preventing me from testing my app.




Environment:

=== Visual Studio Community 2017 for Mac (Preview) ===

Version 7.2 Preview (7.2 build 617)
Installation UUID: 6c4f5aaf-4e3e-4121-aa23-b12d8d6df997
Runtime:
	Mono 5.4.0.199 (2017-06/7d788778005) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 504000199

=== NuGet ===

Version: 4.3.1.4445

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	2.0.0
	1.1.2
	1.1.0
	1.0.5
	1.0.1
SDK: /usr/local/share/dotnet/sdk/2.0.0/Sdks
SDK Versions:
	2.0.0
	1.0.4
	1.0.0-preview2-1-003177
	1.0.0-preview2-003131
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.4.0/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.5.6
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 9.0 (13247)
Build 9A235

=== Xamarin.iOS ===

Version: 11.2.0.4 (Visual Studio Community)
Hash: 9ddb5e6c
Branch: d15-4-xi
Build date: 2017-09-18 09:52:49-0400

=== Xamarin.Android ===

Version: 8.0.0.21 (Visual Studio Community)
Android SDK: /Users/davidd/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.4 (API level 19)
		5.0 (API level 21)
		5.1 (API level 22)
		6.0 (API level 23)
		7.0 (API level 24)
		7.1 (API level 25)
		8.0 (API level 26)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 25.0.5
SDK Build Tools Version: 26.0.0

Java SDK: /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin.Mac ===

Version: 3.8.0.44 (Visual Studio Community)

=== Xamarin Inspector ===

Version: 1.3.1
Hash: cbc48dd
Branch: 1.3-release
Build date: Thu, 21 Sep 2017 19:52:53 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 702000617
Git revision: f857140b11430570fb281ee93973d72ffae223ac
Build date: 2017-09-15 10:19:13-04
Xamarin addins: 140b5e34b39dd79ffde4a708b3a49b1dba0cef1b
Build lane: monodevelop-lion-d15-4

=== Operating System ===

Mac OS X 10.12.6
Darwin 16.7.0 Darwin Kernel Version 16.7.0
    Thu Jun 15 17:36:27 PDT 2017
    root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

MvvmCross Template pack 2.0.1
AddinMaker 1.3.7
FileNesting 0.1.1
NuGet Package Explorer 0.2
NuGet Package Management Extensions 0.12.2
Paket 0.3
xUnit.NET 2 testing framework support 0.7.5
Manifest.addin 0.0.0.0
Comment 1 Stephane Delcroix 2017-09-29 19:42:08 UTC
thanks for this report. I can confirm the reported issue,
we'll work on fixing this quite soon. in the mean time, try to return a GridLength from your OmPlatform (instead of a Double)

Warm regards,

The XF Team
Comment 2 Stephane Delcroix 2017-09-30 14:52:10 UTC
The behavior you're expecting is as follow:

Autoconversion from `OnPlatform<double>` to `double`, then from `double` to `GridLength` using the defined implicit conversion operators defined on `OnPlatform<`1>` and `GridLength`.

Implicit conversions being expected to be lossless, that could make some sense, but finding the right pair (or the best pair) of implicit conversion operator is non-trivial. This is the reason why this syntax doesn't work in C#

(new ColumnDefinition()).Width = new OnPlatform<double>() { iOS = 100, Default = 0 };

Error CS0029: Cannot implicitly convert type 'Xamarin.Forms.OnPlatform<double>' to 'Xamarin.Forms.GridLength' (CS0029)

This behavior is defined in the C# language specification (I have the v5.0 handy) on chapter 6.4.3 (should be the bottom of page 120) in the following terms:

"Evaluation of a user-defined conversion never involves more than one user-defined or lifted conversion operator. In other words, a conversion from type S to type T will never first execute a user-defined conversion from S to X and then execute a user-defined conversion from X to T."

This used to work before because of the special place the OnPlatform<`1> had in my heart, or in the Xaml loader, but that's no longer the case, and that syntax should never have worked at all.

At this point, we're evaluating if we should make this work (because it used to) or leave it as is (because it should never have worked)
Comment 3 Samantha Houts [MSFT] 2017-10-04 16:52:21 UTC
https://github.com/xamarin/Xamarin.Forms/pull/1176
Comment 4 Samantha Houts [MSFT] 2017-10-20 17:53:58 UTC
Should be fixed in 2.5.0-pre2. Thank you!

Note You need to log in before you can comment on or make changes to this bug.