dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.16k stars 1.74k forks source link

MAUI app contains two pages with BlazorWebView control that throw an exception when the second page is called in Android #8502

Closed WenLiHou closed 1 year ago

WenLiHou commented 2 years ago

Description

Due to the csp strategy for external content, I intend to use two blazorwebview controls instead of iframe in my maui blazor hybrid application. But it was found that this app works well in Windows Machine, but throws exceptions in Android. The link below is my sample program that throws exception in Android: https://github.com/WenLiHou/maui-two-blazor-webview.git


Works well in Windows Machine: image


Throws exception in Android: 无标题

Steps to Reproduce

  1. Create a new maui blazor hybrid template program;
  2. Add a navigation link "web view" and a webview page;
  3. Run the program in Android and click on the "web view" navigation link; Throw an exception.

Version with bug

6.0.400 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 9 and up

Did you find any workaround?

No response

Relevant log output

Microsoft.Maui.Platform.ToPlatformException
  Message=Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler found for Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView is incompatible
  Source=Microsoft.Maui
  StackTrace:
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 113
   at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 142
   at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 44
   at Microsoft.Maui.Handlers.ContentViewHandler.MapContent(IContentViewHandler handler, IContentView page) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 49
   at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IContentViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 183
   at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 47
   at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 82
   at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 74
   at Microsoft.Maui.Handlers.ViewHandler.SetVirtualView(IElement element) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandler.cs:line 125
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53
   at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 27
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:line 64
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:line 20
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:line 299
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 105
   at Microsoft.Maui.Platform.ViewExtensions.ToHandler(IView view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ViewExtensions.cs:line 44
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellContentFragment.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellContentFragment.cs:line 145
   at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState) in C:\a\_work\1\s\generated\androidx.fragment.fragment\obj\Release\net6.0-android\generated\src\AndroidX.Fragment.App.Fragment.cs:line 1999
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 352
kristinx0211 commented 2 years ago

verified repro on android with above repro project. works fine on windows.

WenLiHou commented 2 years ago

I use the WebView control instead of the BlazorWebView control, which works fine on Android. But I found another problem.

When switching from the MAUI WebView page back to the BlazorWebView page, this results in a visible flash in the blazor UI.

the repro project: https://github.com/WenLiHou/maui-two-blazor-webview/tree/visible-flash

mkArtakMSFT commented 2 years ago

@Eilon can you please look into this and see what needs to be done? Thanks!

ghost commented 2 years ago

We've moved this issue to the Future milestone. This means that it is not going to be worked on for the coming release. We will reassess the issue following the current release and consider this item at that time.

Eilon commented 1 year ago

Hi @WenLiHou , I investigated the issue that you originally reported and it turns out it's caused by using a Windows back-slash \ instead of a cross-platform forward-slash / when specifying the BlazorWebView's HostPage property in the WebViewPage.xaml file. I sent a PR to fix this in your sample app: https://github.com/WenLiHou/maui-two-blazor-webview/pull/1

I changed from:

    <BlazorWebView HostPage="wwwroot\index.html" UrlLoading="BlazorWebView_UrlLoading">

To:

    <BlazorWebView HostPage="wwwroot/index.html" UrlLoading="BlazorWebView_UrlLoading">
Eilon commented 1 year ago

I use the WebView control instead of the BlazorWebView control, which works fine on Android. But I found another problem.

When switching from the MAUI WebView page back to the BlazorWebView page, this results in a visible flash in the blazor UI.

the repro project: https://github.com/WenLiHou/maui-two-blazor-webview/tree/visible-flash

Regarding this, I tried that repro and didn't see any noticeable flash/flicker. It could be that my machine is too fast to notice it? Either way, if that is still an issue please log a new issue with the repro as well as a video that shows the flicker and any other necessary information.

Given that the original issue was resolved (see my previous comment), and I can't reproduce this other issue, I'm closing this issue.