Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentNullException when dynamically adding/removing root components from BlazorWebView #27171

Open
rhirtz-ores opened this issue Jan 16, 2025 · 1 comment
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@rhirtz-ores
Copy link

Description

While trying to dynamically swap components into an already loaded BlazorWebView I stumbled upon an ArgumentNullException preventing me from altering webview components.

This error is triggered in BlazorWebViewHandler.cs line 119-120. NotifyCollectionChangedEventArgs NewItems/OldItems properties values are casted to RootComponent type regardless of the action operated on the collection.
But the NewItems property will be null when we remove an existing element and OldItems null when we add a new one.

Steps to Reproduce

  1. Create a .Net Maui Blazor Hybrid App
  2. In MainPage.xaml, put the BlazorWebView in a StackLayout, give BlazorWebView a size, and add a Button with a Click event like so:
    <StackLayout>
        <BlazorWebView x:Name="blazorWebView" HostPage="wwwroot/index.html" WidthRequest="200" HeightRequest="200">
            <BlazorWebView.RootComponents>
                <RootComponent Selector="#app" ComponentType="{x:Type local:Components.Routes}" />
            </BlazorWebView.RootComponents>
        </BlazorWebView>

        <Button Text="Remove webview content" Clicked="OnClick"></Button>
    </StackLayout>
  1. In MainPage.xaml.cs code behind implement Click event handler like so:
        public void OnClick(object? sender, EventArgs e)
        {
            blazorWebView.RootComponents.RemoveAt(0);
        }
  1. Launch the app and click the button

Result: ArgumentNullException is thrown and BlazorWebView component is still displayed

Link to public reproduction project repository

No response

Version with bug

8.0.71 SR7.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

windows 10.0.19041.0

Did you find any workaround?

No response

Relevant log output

Eception stack trace:

   at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) in System.Linq\ThrowHelper.cs:line 10
   at System.Linq.Enumerable.Cast[TResult](IEnumerable source) in System.Linq\Enumerable.cs:line 4278
   at Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.<>c__DisplayClass16_0.<<OnRootComponentsCollectionChanged>b__0>d.MoveNext() in Microsoft.AspNetCore.Components.WebView.Maui\BlazorWebViewHandler.cs:line 113
   at Microsoft.Maui.Dispatching.DispatcherExtensions.<>c__DisplayClass3_0.<<DispatchAsync>b__0>d.MoveNext()
   at Microsoft.Maui.Dispatching.DispatcherExtensions.<>c__DisplayClass2_0`1.<<DispatchAsync>b__0>d.MoveNext()
@rhirtz-ores rhirtz-ores added the t/bug Something isn't working label Jan 16, 2025
@Zhanglirong-Winnie Zhanglirong-Winnie added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jan 16, 2025
@Zhanglirong-Winnie
Copy link

This issue has been verified Visual Studio 17.13 Preview 2.1 (9.0.30 & 9.0.0 & 8.0.71 &8.0.60). Can repro this issue on windows platform.

@StephaneDelcroix StephaneDelcroix added the area-blazor Blazor Hybrid / Desktop, BlazorWebView label Jan 16, 2025
@StephaneDelcroix StephaneDelcroix added this to the Backlog milestone Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants