fsprojects / Avalonia.FuncUI

Develop cross-plattform GUI Applications using F# and Avalonia!
https://funcui.avaloniaui.net/
MIT License
889 stars 72 forks source link

Try to get the control catalog working in NativeAOT builds #423

Open Numpsy opened 1 month ago

Numpsy commented 1 month ago

The main issue besides the dynamically loaded xaml being trimmed out (avoided for now by rooting the control catalog assembly) is that it seems to not like using sprintf in views, e.g.

Unhandled Exception: System.NotSupportedException: 'Microsoft.FSharp.Core.PrintfImpl+Specializations`3[Microsoft.FSharp.Core.Unit,System.String,System.String].CaptureFinal1[System.Int32](Microsoft.FSharp.Core.PrintfImpl+Step[])' is missing native code. MethodInfo.MakeGenericMethod() is not compatible with AOT compilation. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
   at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo`1.GetUncachedMethodInvoker(RuntimeTypeInfo[], MemberInfo) + 0x2f
   at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo`1.MakeGenericMethod(Type[]) + 0x189
   at Microsoft.FSharp.Core.PrintfImpl.FormatParser`4.buildCaptureFunc[a](PrintfImpl.FormatSpecifier, a, Type[], Type, Tuple`5) + 0x21b
   at Microsoft.FSharp.Core.PrintfImpl.FormatParser`4.parseAndCreateFuncFactoryAux(FSharpList`1, String, Type, Int32&) + 0x22c
   at Microsoft.FSharp.Core.PrintfImpl.FormatParser`4.parseAndCreateFunctionFactory() + 0x78
   at Microsoft.FSharp.Core.PrintfImpl.FormatParser`4.GetCurriedPrinterFactory() + 0x1f
   at Microsoft.FSharp.Core.PrintfImpl.FormatParser`4.GetCurriedStringPrinter() + 0x20
   at Avalonia.FuncUI.ControlCatalog.Views.DataTemplateDemo.view(DataTemplateDemo.State state, FSharpFunc`2 dispatch) + 0xae
   at Avalonia.FuncUI.ControlCatalog.Views.MainView.view(MainView.CounterState state, FSharpFunc`2 dispatch) + 0x64e
   at Avalonia.FuncUI.Elmish.Program.withHost@14.Invoke(model, FSharpFunc`2) + 0x78
   at Elmish.ProgramModule.runWithDispatch[msg,arg,model,view](FSharpFunc`2, arg, Program`4) + 0x200
   at Avalonia.FuncUI.ControlCatalog.App.OnFrameworkInitializationCompleted() + 0x11c
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder, String[], Action`1) + 0x2f
   at Avalonia.FuncUI.ControlCatalog.Desktop!<BaseAddress>+0xa51d70
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.NotSupportedException: 'Microsoft.FSharp.Core.PrintfImpl+Specializations`3[Microsoft.FSharp.Core.Unit,System.String,System.String].CaptureFinal1[System.Int32](Microsoft.FSharp.Core.PrintfImpl+Step[])' is missing native code. MethodInfo.MakeGenericMethod() is not compatible with AOT compilation. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
   at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo`1.GetUncachedMethodInvoker(RuntimeTypeInfo[], MemberInfo) + 0x2f
   at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo`1.MakeGenericMethod(Type[]) + 0x189
   at Microsoft.FSharp.Core.PrintfImpl.FormatParser`4.buildCaptureFunc[a](PrintfImpl.FormatSpecifier, a, Type[], Type, Tuple`5) + 0x21b
   at Microsoft.FSharp.Core.PrintfImpl.FormatParser`4.parseAndCreateFuncFactoryAux(FSharpList`1, String, Type, Int32&) + 0x22c
   at Microsoft.FSharp.Core.PrintfImpl.FormatParser`4.parseAndCreateFunctionFactory() + 0x78
   at Microsoft.FSharp.Core.PrintfImpl.FormatParser`4.GetCurriedPrinterFactory() + 0x1f
   at Microsoft.FSharp.Core.PrintfImpl.FormatParser`4.GetCurriedStringPrinter() + 0x20
   at Avalonia.FuncUI.ControlCatalog.Views.GridDemo.view(GridDemo.State state, FSharpFunc`2 dispatch) + 0x52c

But changing those to use interpolated strings rather than sprintf seems to fix it - after that it seems to run ok (On Windows at least - haven't tried it on anything else yet)