WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

WPF+UWP from 1.0.1661.34 to 1.0.1777-prerelease Exception

Open xiakan opened this issue 2 years ago • 44 comments

Description WPF+UWP
Upgrading from1.0.1661.34 to 1.0.1777-prerelease ,throw an exception

Version SDK: 1.0.1661.34 Runtime: NET.Core 3.1 Framework: WPF OS: Win11

file1 file2

AB#44611801

xiakan avatar Apr 18 '23 07:04 xiakan

Thanks for reaching out. I've assigned this to a dev that can help follow up on this.

novac42 avatar Apr 19 '23 07:04 novac42

Look forward to your reply

xiakan avatar Apr 20 '23 06:04 xiakan

@novac42 Is anyone responding to this question?Look forward to your reply

xiakan avatar Apr 23 '23 01:04 xiakan

Hi @xiakan,

Could you provide the code that is causing the problem?

plantree avatar Apr 23 '23 05:04 plantree

@plantree https://github.com/xiakan/WPFAndUWPDemo.git Please change the platform to x64 during runtime,Thanks

xiakan avatar Apr 24 '23 06:04 xiakan

Hi @xiakan,

Thanks for your demo. I'll check ASAP.

plantree avatar Apr 26 '23 03:04 plantree

This one seems to be a UWP related issue. Adding another dev to look into it.

novac42 avatar Apr 26 '23 06:04 novac42

Apologies, how do you make the error occur? Can you show the code that is running when the exception occurs?

david-risney avatar May 01 '23 18:05 david-risney

There will be exceptions when running after re pulling the code. Thanks.

xiakan avatar May 05 '23 03:05 xiakan

May I ask if you see the wrong place?

xiakan avatar May 06 '23 07:05 xiakan

@david-risney May I ask if anyone is tracking this issue?

xiakan avatar May 09 '23 03:05 xiakan

@novac42 Do you still have developers to track this issue?

xiakan avatar May 10 '23 02:05 xiakan

Apologies, how do you make the error occur? Can you show the code that is running when the exception occurs?

Hi @xiakan,

If you could provide some core code or a complete .sln project, that would be helpful.

plantree avatar May 10 '23 02:05 plantree

@plantree https://github.com/xiakan/WPFAndUWPDemo.git Please change the platform to x64 during runtime,Thanks

xiakan avatar May 10 '23 02:05 xiakan

@plantree https://github.com/xiakan/WPFAndUWPDemo.git Please change the platform to x64 during runtime,Thanks

I'm so sorry for my forget.

Hi @david-risney, this is the demo project. I have tested it, and it might be a WinRT related.

plantree avatar May 10 '23 03:05 plantree

So what's the solution?

xiakan avatar May 10 '23 03:05 xiakan

Thanks, let me try this again.

david-risney avatar May 10 '23 16:05 david-risney

The reason for the exception with 1.0.1777 is that WinRT code in XAML is trying to load Microsoft.Web.WebView2.Core.dll but it is finding the .NET DLL rather than the WinRT DLL in the Debug folder.

In the WebView2 nuget package there is a Microsoft.Web.WebView2.Core.dll for .NET and Microsoft.Web.WebView2.Core.dlls for WinRT in different paths within the nuget package. XAML is using WinRT so needs to load the WinRT one, but the .NET one has been copied to the Debug folder and is what WinRT is trying to load, which does not work. In 1.0.1661.34 the correct DLL is in the Debug folder.

I have tried it with recent SDKs and I find it is only not working in 1777:

WebView2 SDK version Working
1.0.1661.34
1.0.1774.30
1.0.1777-prerelease
1.0.1829-prerelease

Your solution has the application project using .NET and the class library dependency project using WinRT which is how I think it is confusing the WebView2 SDK package and copying the incorrect DLL, but this seems to only be an issue in 1777. Hopefully you can use 1829 with no issue.

david-risney avatar May 10 '23 18:05 david-risney

Other class libraries in our project use Microsoft. Web. WebView2. Wpf. WebView2. When updated to 1.0.1829 pre release, an exception will be thrown as follows: image

May I ask how to solve the exception thrown above? This problem has been bothering me for a long time and I hope to receive a response. Thanks

xiakan avatar May 11 '23 02:05 xiakan

This sounds like the opposite issue. This could perhaps happen if you have a copy of the WinRT Microsoft.Web.WebView2.Core.DLL in your debug folder and .NET is trying to load the .NET Microsoft.Web.WebView2.Core.DLL. The issue is trying to mix both .NET and WinRT WebView2 usage in the same solution. Offhand I would think it should be possible, but I don't think we've validated this in particular.

@champnic do you know if we've made any changes around this area in recent nuget packages? I diffed the targets of different versions and didn't see anything obvious.

david-risney avatar May 11 '23 17:05 david-risney

No changes in this space as far as I'm aware. Sounds like this might just be a scenario (.NET + UWP) that we haven't validated, and doesn't look fully supported yet. Could be chance or timing or something that's causing it to load one first, and then break when it's the wrong version for elsewhere. It might also be limited to the prerelease versions? @xiakan does the latest release SDK still work, or is that broken for you as well? And if you go back to 1.0.1661.34 does it start working again?

champnic avatar May 11 '23 19:05 champnic

@champnic Returning to 1.0.1661.34, it will start working again, but every upgrade will cause trouble. The version of Microsoft. UI. Xaml. Controls. WebView2 in the project cannot be higher than Microsoft Web. WebView2. Wpf. WebView2, currently Microsoft Web. WebView2. Wpf. The SDK for WebView2 is only available at 1.0.1724 pre release, so Microsoft. UI. Xaml. Controls. WebView2 cannot be updated to the latest version.

xiakan avatar May 12 '23 01:05 xiakan

@david-risney Please consult, in the NET Core 3.1+UWP scenario, attempting to load web page content will flash white, followed by loading content, which is obvious under black themes. https://github.com/xiakan/WPFAndUWPDemo.git

xiakan avatar May 12 '23 02:05 xiakan

Thanks for checking. I've added this bug to our backlog.

For the white flash, you can set the default background color so that instead of white you can show black, or transparent, or some other dark color that matches the theming. On UWP you need to use the environment variable documented here: https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2controller2?view=webview2-1.0.1774.30#get_defaultbackgroundcolor

champnic avatar May 12 '23 18:05 champnic

Thank you for your prompt reply. If it's fixed, let me know, okay?

Additionally, I am using Microsoft. UI. Xaml. Controls. Webview2, and there is no defaultBackgroundColor attribute here. How to set the default background color for Microsoft. UI. Xaml. Controls. Webview2?

xiakan avatar May 15 '23 02:05 xiakan

It's the environment variable section in the doc I linked: "This value may also be set by using the WEBVIEW2_DEFAULT_BACKGROUND_COLOR environment variable. There is a known issue with background color where setting the color by API can still leave the app with a white flicker before the DefaultBackgroundColor takes effect. Setting the color via environment variable solves this issue. The value must be a hex value that can optionally prepend a 0x. The value must account for the alpha value which is represented by the first 2 digits. So any hex value fewer than 8 digits will assume a prepended 00 to the hex value and result in a transparent color. get_DefaultBackgroundColor will return the result of this environment variable if used. This environment variable can only set the DefaultBackgroundColor once. Subsequent updates to background color must be done through API call."

In UWP you can use Environment.SetEnvironmentVariable("WEBVIEW2_DEFAULT_BACKGROUND_COLOR", "0xFFRRGGBB"); with your desired RGB values.

champnic avatar May 15 '23 20:05 champnic

Hi @xiakan,

I have tried by Visual Studio 2019 locally, and it seems fine.

Before using 1.0.1777-prerelease, please clean the bin\ and .vs\ first. If there is still a problem, please take a screenshot about bin\Wonderbar\x64\Debug. Thanks.

plantree avatar May 16 '23 06:05 plantree

@plantree I have tried this project before, but I can't. https://github.com/xiakan/WPFAndUWPDemo.git image

xiakan avatar May 16 '23 07:05 xiakan

If you have time, you can give it a try.Thanks.

xiakan avatar May 16 '23 07:05 xiakan

These files should not exist since you don't use it.

image

Which Visual Studio are you using? 19 or 22? Try to use 19.

plantree avatar May 16 '23 07:05 plantree