Open KRA2008 opened 6 months ago
We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.
cc @StephaneDelcroix
XamlC generates the following code:
// Microsoft.Maui.Graphics.Rect rect = default(Microsoft.Maui.Graphics.Rect);
IL_0001: ldloca.s 0
IL_0003: initobj [Microsoft.Maui.Graphics]Microsoft.Maui.Graphics.Rect
// ResourceDictionary resourceDictionary = new ResourceDictionary();
IL_0009: newobj instance void [Microsoft.Maui.Controls]Microsoft.Maui.Controls.ResourceDictionary::.ctor()
IL_000e: stloc.1
// ...
// rect.X = 0.0;
IL_0055: ldloca.s 0
IL_0057: ldc.r8 0.0
IL_0060: call instance void [Microsoft.Maui.Graphics]Microsoft.Maui.Graphics.Rect::set_X(float64)
// rect.Y = 1.0;
IL_0065: ldloca.s 0
IL_0067: ldc.r8 1
IL_0070: call instance void [Microsoft.Maui.Graphics]Microsoft.Maui.Graphics.Rect::set_Y(float64)
// rect.Width = 1.0;
IL_0075: ldloca.s 0
IL_0077: ldc.r8 1
IL_0080: call instance void [Microsoft.Maui.Graphics]Microsoft.Maui.Graphics.Rect::set_Width(float64)
// rect.Height = 1.0;
IL_0085: ldloca.s 0
IL_0087: ldc.r8 1
IL_0090: call instance void [Microsoft.Maui.Graphics]Microsoft.Maui.Graphics.Rect::set_Height(float64)
// resourceDictionary.Add("iBreakTheApp", (Android.Graphics.Rect)rect);
IL_0095: ldloc.1
IL_0096: ldstr "iBreakTheApp"
IL_009b: ldloc.0
IL_009c: call class [Mono.Android]Android.Graphics.Rect [Microsoft.Maui.Graphics]Microsoft.Maui.Graphics.Rect::op_Implicit(valuetype [Microsoft.Maui.Graphics]Microsoft.Maui.Graphics.Rect)
IL_00a1: callvirt instance void [Microsoft.Maui.Controls]Microsoft.Maui.Controls.ResourceDictionary::Add(string, object)
// ...
// staticResourceExtension.Key = "iBreakTheApp";
IL_012c: ldloc.2
IL_012d: ldstr "iBreakTheApp"
IL_0132: callvirt instance void [Microsoft.Maui.Controls.Xaml]Microsoft.Maui.Controls.Xaml.StaticResourceExtension::set_Key(string)
// object obj = ((IMarkupExtension)staticResourceExtension).ProvideValue((IServiceProvider)xamlServiceProvider);
IL_0137: ldloc.2
// ...
// label.SetValue(AbsoluteLayout.LayoutBoundsProperty, (Microsoft.Maui.Graphics.Rect)obj);
IL_021f: ldloc.3
IL_0220: ldsfld class [Microsoft.Maui.Controls]Microsoft.Maui.Controls.BindableProperty [Microsoft.Maui.Controls]Microsoft.Maui.Controls.AbsoluteLayout::LayoutBoundsProperty
IL_0225: ldloc.s 10
IL_0227: unbox.any [Microsoft.Maui.Graphics]Microsoft.Maui.Graphics.Rect
IL_022c: box [Microsoft.Maui.Graphics]Microsoft.Maui.Graphics.Rect
IL_0231: callvirt instance void [Microsoft.Maui.Controls]Microsoft.Maui.Controls.BindableObject::SetValue(class [Microsoft.Maui.Controls]Microsoft.Maui.Controls.BindableProperty, object)
// ...
For some reason it creates Microsoft.Maui.Graphics.Rect
but it converts it into Android.Graphics.Rect
using implicit operator before storing it in ResourceDictionary
. When it's about to call SetValue
, it tries to convert it back to Microsoft.Maui.Graphics.Rect
using unbox.any
and I assume that's what fails.
Description
I get an "InvalidCastException" when I have an element inside an AbsoluteLayout with its LayoutBounds bound to a Rect defined in a ResourceDictionary and XamlC is enabled on Android. This only happens on Android, it does not happen on iOS. Here is an extremely simple code example that causes the problem:
It happens in Release configuration, but it also happens in Debug configuration with defaults except with XamlC enabled.
Steps to Reproduce
No response
Link to public reproduction project repository
https://github.com/KRA2008/MAUIBoundsBoundToRectOnReleaseAndroid.git
Version with bug
8.0.7 SR2
Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
No response
Did you find any workaround?
No response
Relevant log output