maui icon indicating copy to clipboard operation
maui copied to clipboard

XamlParseException when using an Int32 value for a RadioButton

Open jskeet opened this issue 2 years ago • 9 comments

Description

I'm trying to bind some RadioButton views using System.Int32 values and separate display labels. I'm using <RadioButton.Value> in the XAML with the appropriate values.

On Android and Windows, this works fine. On iOS, it crashes with a XamlParseException.

Steps to Reproduce

  1. Create a new MAUI app
  2. Edit MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             x:Class="ReproInt32Combo.MainPage">

    <ScrollView>
        <VerticalStackLayout>
            <RadioButton Content="First">
                <RadioButton.Value>
                    <sys:Int32>1</sys:Int32>
                </RadioButton.Value>
            </RadioButton>
            <RadioButton Content="Second">
                <RadioButton.Value>
                    <sys:Int32>2</sys:Int32>
                </RadioButton.Value>
            </RadioButton>
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
  1. Delete the C# code in MainPage.xaml.cs (except for the constructor)
  2. Run on Windows or Android - no issues
  3. Run on iOS and observe the exception below
  4. Try x:Int32 instead of sys:Int32 and observe the same behavior

Exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> Microsoft.Maui.Controls.Xaml.XamlParseException: Position 11:22. Type sys:Int32 not found in xmlns clr-namespace:System;assembly=mscorlib
   at Microsoft.Maui.Controls.Xaml.CreateValuesVisitor.Visit(ElementNode node, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Visit(RootNode rootnode, HydrationContext visitorContext, Boolean useDesignProperties)
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Assembly rootAssembly, Boolean useDesignProperties)
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Boolean useDesignProperties)
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, Type callingType)
   at Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml[MainPage](MainPage view, Type callingType)
   at ReproInt32Combo.MainPage.InitializeComponent() in C:\Users\jon\Issues\AtYourService\MauiIpad\ReproInt32Combo\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\MainPage.xaml.sg.cs:line 22
   at ReproInt32Combo.MainPage..ctor() in C:\Users\jon\Issues\AtYourService\MauiIpad\ReproInt32Combo\MainPage.xaml.cs:line 7
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object , Object[] , Boolean )
   --- End of inner exception stack trace ---
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object , Object[] , Boolean )
   at System.RuntimeType.CreateInstanceMono(Boolean , Boolean )
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean , Boolean )
   at System.Activator.CreateInstance(Type , Boolean , Boolean )
   at System.Activator.CreateInstance(Type , Boolean )
   at System.Activator.CreateInstance(Type )
   at Microsoft.Maui.Controls.ShellContent.<>c__DisplayClass19_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b__0()
   at Microsoft.Maui.Controls.ElementTemplate.CreateContent()
   at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container)
   at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.LoadRenderers()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad()
   at UIKit.UIApplication.Main(String[] , Type , Type )
   at ReproInt32Combo.Program.Main(String[] args) in [...]

With x:Int32, the exception contains ---> Microsoft.Maui.Controls.Xaml.XamlParseException: Position 11:22. Type x:Int32 not found in xmlns http://schemas.microsoft.com/winfx/2009/xaml instead.

I've tried with the linker disabled, but the debugger didn't even start then. (That could be an environmental issue.) I wouldn't expect System.Int32 to be stripped anyway though... it's a pretty fundamental type!

Version with bug

6.0.486 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 15.6

Did you find any workaround?

No.

Relevant log output

No response

jskeet avatar Aug 16 '22 18:08 jskeet

Bizarrely, I've just run this not in the debugger, and in Release mode, and it doesn't crash. It's slightly tricky to know whether it's successfully binding, but it's at least not crashing...

jskeet avatar Aug 16 '22 18:08 jskeet

Just tried this in my real app (using sys:Int32) and the binding does indeed appear to work. Happy to try any further experiments that are suggested (linker and LLVM options?) but I'd be really working in the dark without suggestions.

jskeet avatar Aug 16 '22 18:08 jskeet

I can confirm this bug. It crashes on device with any type from Sys: bool, date, int... in debug mode. Works fine on simulator

yurkinh avatar Aug 17 '22 07:08 yurkinh

We have the same issue, but with a custom control. Means that our iOS app is unrunnable. If this could be top priority to fix please.

The outputted log: 2022-08-25 10:56:27.406805+1200 TMM[49043:1756968] Unhandled managed exception: Position 9:8. Type pages:InvertBooleanConverter not found in xmlns clr-namespace:<ProjectName>.Pages (Microsoft.Maui.Controls.Xaml.XamlParseException) at Microsoft.Maui.Controls.Xaml.CreateValuesVisitor.Visit(ElementNode , INode ) at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor , INode ) at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor , INode ) at Microsoft.Maui.Controls.Xaml.RootNode.Accept(IXamlNodeVisitor , INode ) at Microsoft.Maui.Controls.Xaml.XamlLoader.Visit(RootNode , HydrationContext , Boolean ) at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object , String , Assembly , Boolean ) at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object , String , Boolean ) at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object , Type ) at Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml[App](App , Type ) at TMM.App.InitializeComponent() in C:\Users\Documents\developmen t\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\App.xaml.sg.cs:line 22 at TMM.App..ctor() in C:\Users\Documents\development\App.xaml.cs:line 16 at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object , Object[] , Boolean ) at System.Reflection.RuntimeConstructorInfo.DoInvoke(Object , BindingFlags , Binder , Object[] , CultureInfo ) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags , Binder , Object[] , CultureInfo ) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext ) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, Pu blicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite , RuntimeResolverContext ) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite , RuntimeResolverContext ) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite , RuntimeResolverContext ) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite , ServiceProviderEngineScope ) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type ) at System.Collections.Concurrent.ConcurrentDictionary2[[System.Type, System.Private.CoreLib, Version=6.0.0.0, Culture=n eutral, PublicKeyToken=7cec85d7bea7798e],[System.Func2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope, Microsoft.Extensions.DependencyInjection, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].GetOrAdd(Type , Func2 ) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type , ServiceProviderEngineScope ) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type ) at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type ) at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type ) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider , Type ) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRe quiredServiceIApplication at Microsoft.Maui.MauiUIApplicationDelegate.FinishedLaunching(UIApplication , NSDictionary ) at UIKit.UIApplication.Main(String[] , Type , Type ) at <ProjectName>.Program.Main(String[] ) in C:\Users\Documents\development\Platforms\iOS\Program.cs:line 13`

Greg-Bates-Trimble avatar Aug 24 '22 22:08 Greg-Bates-Trimble

@StephaneDelcroix

PureWeen avatar Oct 18 '22 18:10 PureWeen

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Oct 18 '22 18:10 ghost

Just tried this in my real app (using sys:Int32) and the binding does indeed appear to work. Happy to try any further experiments that are suggested (linker and LLVM options?) but I'd be really working in the dark without suggestions.

in Release, the XAML is compiled, this is probably why it works there

@jskeet does it happens on both simulator and device ?

StephaneDelcroix avatar Oct 19 '22 13:10 StephaneDelcroix

@StephaneDelcroix: I'm afraid I can't easily check at the moment. But I suspect given my earlier comment of "Bizarrely, I've just run this not in the debugger, and in Release mode, and it doesn't crash" that it was then working in the simulator. Basically I think it's "debug=boom, release=fine" regardless of simulator vs device.

jskeet avatar Oct 19 '22 13:10 jskeet

same problem for me, application in debug mode run without problem, in release in the emulator crash with a similar error:

AndroidRuntime android.runtime.JavaProxyThrowable: Microsoft.Maui.Controls.Xaml.XamlParseException: Position 109:30. Type Int32 not found in xmlns http://schemas.microsoft.com/winfx/2009/xaml The source code is:

                        <!--<Button.CornerRadius>
                            <OnPlatform x:TypeArguments="x:Int32">
                                <On Platform="iOS" Value="12" />
                                <On Platform="Android" Value="10" />
                            </OnPlatform>
                        </Button.CornerRadius>-->

fgiacomelli avatar Nov 11 '22 13:11 fgiacomelli

I'm seeing this as well (on iOS). I've successfully ported my XF app to Android, but the iOS port is stuck on this.

gkarabin avatar Jan 01 '23 16:01 gkarabin

I'm noticing a similar issue receiving the XamlParseException as well w/ MAUI for .NET 6 on UWP (v10.0.19041.0). My app will run with Debug mode, however, not in Release. Which is concerning when we go to release to the market.

The most troublesome piece is, the debugger isn't catching the error to help me track down where the issue is, just this message in the output window (below). I have all "Exception Settings" checked for items pertaining to this exception too.

Exception thrown: 'Microsoft.Maui.Controls.Xaml.XamlParseException' in Microsoft.Maui.Controls.Xaml.dll

Related

I did further research and found #11534 where it suggested to add the following to the csproj which was most helpful. Guess I'm surprised that XamlCompilationOptions.Compile didn't pick up on this.

<_MauiForceXamlCForDebug>true</_MauiForceXamlCForDebug>

UPDATE

Warning, _MauiForceXamlCForDebug broke Hot Reload in our MAUI .NET 6 projects

DamianSuess avatar Jan 27 '23 18:01 DamianSuess

One thing to note here: xmlns:sys="clr-namespace:System;assembly=mscorlib"

mscorlib.dll in .NET 5+ is an assembly that only contains type forwards. For example:

ilspy 'C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Runtime.Mono.android-arm64\7.0.4\runtimes\android-arm64\lib\net7.0\mscorlib.dll'

[assembly: TypeForwardedTo(typeof(Int32))]

Where System.Int32 is actually in System.Private.CoreLib.dll.

In builds that use PublishTrimmed=true it can get trimmed away and the type forwards disappear. Call sites will be updated with the "real" assembly. It kind of makes sense that you'd see different behavior in Debug vs Release (iOS has to run the trimmer even in Debug mode, while Android & Windows do not).

I think a solution is to special case mscorlib when parsing XAML and try locating System.Int32 from a different BCL assembly.

A workaround might be to put:

xmlns:sys="clr-namespace:System;assembly=System.Private.CoreLib"

jonathanpeppers avatar Mar 30 '23 14:03 jonathanpeppers

So, what can i do to with this problem before you release new version ? I got the same problem!

dongfo avatar Apr 19 '23 04:04 dongfo

Hello lovely human, thank you for your comment on this issue. Because this issue has been closed for a period of time, please strongly consider opening a new issue linking to this issue instead to ensure better visibility of your comment. Thank you!

ghost avatar Apr 19 '23 04:04 ghost