dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.36k stars 4.75k forks source link

[NativeAOT] Blazor Hybrid + Windows Forms event bindings after AOT compilation #109888

Closed dadavadd closed 33 minutes ago

dadavadd commented 2 hours ago

When using Blazor Hybrid with Windows Forms and NativeAOT compilation, event bindings (@onclick etc.) stop working although the UI renders correctly. The binds work correctly without NativeAOT.

<Project Sdk="Microsoft.NET.Sdk.Razor">

  <PropertyGroup>
      <OutputType>WinExe</OutputType>
      <TargetFramework>net8.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>

      <PublishAot>true</PublishAot>
      <_SuppressWinFormsTrimError>true</_SuppressWinFormsTrimError>
      <CustomResourceTypesSupport>true</CustomResourceTypesSupport>
      <MetadataUpdaterSupport>false</MetadataUpdaterSupport>
      <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
      <JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
  </PropertyGroup>

xml files that I used in my project: https://pastebin.com/raw/vhW37bYT NativeAOT compiling logs: logs.txt

MainForm.cs:

    public partial class MainForm : Form
    {
        private BlazorWebView _blazorWebView;
        public MainForm()
        {
            InitializeComponent();

            var services = new ServiceCollection();
            services.AddWindowsFormsBlazorWebView();

            _blazorWebView = new BlazorWebView
            {
                HostPage = "wwwroot/index.html",
                Services = services.BuildServiceProvider(),
            };

            _blazorWebView.RootComponents.Add<Main>("#app");

            _blazorWebView.Dock = DockStyle.Fill;

            Controls.Add(_blazorWebView);
        }
    }

Main.razor:

@using System.Windows.Forms;

<h1>Hello World!</h1>
<button @onclick="OnClickButton">Hello!</button>

@code {
    private void OnClickButton()
    {
        MessageBox.Show("Hello WOrld!");
    }
}
dotnet-policy-service[bot] commented 2 hours ago

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas See info in area-owners.md if you want to be subscribed.

jkotas commented 33 minutes ago

Neither WinForms or Blazor are compatible with full trimming and native AOT currently. It is expected that it is not going to work well. You can follow related issues in WinForms and ASP.NET repos: https://github.com/dotnet/winforms/issues/4649, https://github.com/dotnet/aspnetcore/issues/49409, https://github.com/dotnet/aspnetcore/issues/51598

xml files that I used in my project: https://pastebin.com/raw/vhW37bYT

Manual authoring of RdXml files is not supported https://github.com/dotnet/runtime/blob/main/src/coreclr/nativeaot/docs/rd-xml-format.md . It results into poorly performing apps with hard to diagnose functionality issues as you have found. I am sorry we are not going to help you with authoring of RdXml files to make things "work". It is not a time well spent.