maui icon indicating copy to clipboard operation
maui copied to clipboard

[Windows] Blazor Hybrid flickers on launch (white, then gray background)

Open anth-git opened this issue 2 years ago • 6 comments

Description

When Blazor Hybrid app is launched on Windows, MAUI displays two additional screens before showing Blazor content:

  1. White blank screen
  2. Gray blank screen
  3. Only now Blazor content is displayed

All happens in a fraction of a second causing effect of flickering. Is there any way to eliminate this effect, or at least set some fixed background color to first two screens?

1 2 3 4

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.5

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

Did you find any workaround?

No response

Relevant log output

No response

anth-git avatar Jan 17 '24 12:01 anth-git

Verified this on 8.0.6. Repro on Windows 11 with .NET MAUI Blazor Hybird App template: MauiApp9.zip

XamlTest avatar Feb 29 '24 09:02 XamlTest

Hi, what I saw when starting MAUI Blazor Hybrid template app is that the first blank window is outside of the second one (which is app), it opens and closes quickly. I believe every BlazorWebView and/or WebView initialization causes that "effect", but I am not sure.

Startup sequence

First blank window:

MauiStartup1

Second window is actually app itself. Sometimes, shortly, both windows are on the screen together.

MauiStartup2
    <ItemGroup>
        <PackageReference Include="Microsoft.Maui.Controls" Version="8.0.91" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.91" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="8.0.91" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
    </ItemGroup>

OS: Windows 10 & Windows 11

dekipet avatar Sep 28 '24 08:09 dekipet

This flashing window at startup seems new to me. I have observed this just for a couple of days, and I work with Blazor Hybrid everyday (it is my job). Is there any workaround?

HNordhaus avatar Sep 28 '24 14:09 HNordhaus

The problem has gone (for my .NET MAUI Blazor Hybrid App) after updating .NET 8.0.400 to the current version .NET 8.0.402

HNordhaus avatar Oct 02 '24 07:10 HNordhaus

Hi, if u need a work around, u can first set the blazorwebview visibilty to false and wait for like 2.5 secs or so and then make it visible. but its a hard coded delay it will still flash on slower devices. like so

private void MainPage_Loaded(object? sender, EventArgs e)
{
    Task.Delay(2500).ContinueWith(_ =>
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            blazorWebView.IsVisible = true;
        });
    });

    WebviewInstance = blazorWebView;
}

or A better solution 😊

<BlazorWebView
    x:Name="blazorWebView"
    Background="#333333"
    HostPage="wwwroot/index.html"
    IsVisible="False">
    <BlazorWebView.RootComponents>
        <RootComponent ComponentType="{x:Type components:Routes}" Selector="#app" />
    </BlazorWebView.RootComponents>
</BlazorWebView>
public partial class MainPage : ContentView
{
    public static BlazorWebView? WebviewInstance { get; set; }
    public MainPage()
    {
        InitializeComponent();
        this.Loaded += MainPage_Loaded;
    }

    private void MainPage_Loaded(object? sender, EventArgs e)
    {
        WebviewInstance = blazorWebView;
    }

    [JSInvokable]
    public static void ShowBlazorWebView()
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            WebviewInstance!.IsVisible = true;
        });
    }
}

and then in index.html

<script>
    function notifyBlazorReady() {
        DotNet.invokeMethodAsync('YourAppName', 'ShowBlazorWebView');
    }
</script>

change YourAppName and finally in MainLayout

@inject IJSRuntime JSRuntime
protected override void OnAfterRender(bool firstRender)
{
    if (firstRender)
    {
        JSRuntime.InvokeVoidAsync("notifyBlazorReady");
    }
}

hope this helps ..!!!

muni20 avatar Jul 04 '25 23:07 muni20

https://github.com/dotnet/maui/issues/19942#issuecomment-2387762152

kubaflo avatar Oct 27 '26 18:10 kubaflo