MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
437 stars 51 forks source link

CreateCoreWebView2ControllerAsync can't get a await return #3617

Closed 1CDE683405CAC00EAD7242DCD02BC0AB closed 1 year ago

1CDE683405CAC00EAD7242DCD02BC0AB commented 1 year ago

I want to use WebView2 in .Net6 classlib without UI framework. And I write some code in method like below

var webView2Environment = await CoreWebView2Environment.CreateAsync(null, null, null);
var webView2Controller = await webView2Environment.CreateCoreWebView2ControllerAsync(AuthBase.HWND_MESSAGE);

then I debug the code and meet a problem that CreateCoreWebView2ControllerAsync can't get a await return. I found the same issue#202.

According to that answer, I need to add a dispatcher, but the class of Dispatcher is located in "WindowsBase.dll", because my classlib's targetframe is ".Net6", not ".Net6-windows", so I can't reference the dll into my classlib.

After asked google, I found that If I really want to reference the "WindowsBase.dll", I have to change my classlib targetframe to ".Net6-windows" and enable "UseWPF". but in this case ,I will lose the ability of cross-platform and It's also not really a UIframework-less.

I found the roadmap of webview2 said there will have the MacOS/Linux Preview. I'm very confused If that is true, how can I get a dispatcher in Linux platform in the future?

But that's not the most important, at the moment, I wonder if there is another way to introduce a dispatcher to my classlib or another way to pass through the CreateCoreWebView2ControllerAsync. If the solution can retain the ability of cross-platform, that is the best.

By the way, I can't seem to find a UIframework-less sample in the Webview2 samples repository, maybe it should add one.

novac42 commented 1 year ago

Thanks for reporting the issue and I've assigned a dev who can help follow up on this. We will also take your suggestion on UI-less sample into consideration.

yunate commented 1 year ago

Hi, @1CDE683405CAC00EAD7242DCD02BC0AB! Thanks for your feedback. I'm sorry for the delay in answering. I took two weeks off due to a lumbar surgery

The WV2's API need to run in a UI thread. And could you help to test that set the thread into a UI thread to seed if it can work well? Could use Thread.SetApartmentState(ApartmentState.STA); and then in the thread call Dispatcher.Run(); to set the thread into a UI thread.

_uiThread = new Thread(() =>
{
    Dispatcher.Run();
});
// Requires a Single-Threaded Apartment (STA) COM threading model on its UI thread.
_uiThread.SetApartmentState(ApartmentState.STA);
_uiThread.Start();

// run in the UI thread.
Dispatcher.FromThread(_uiThread).BeginInvoke(
async () => {
    var webView2Environment = await CoreWebView2Environment.CreateAsync(null, null, null);
    var webView2Controller = await webView2Environment.CreateCoreWebView2ControllerAsync(AuthBase.HWND_MESSAGE);
})

// ...
1CDE683405CAC00EAD7242DCD02BC0AB commented 1 year ago

Sorry for replying so late. In actually ,I have solved the problem from issue #202 at the day it was asked. I just want to keep the ability of cross-platform, but now it seems not possible because of the Dispatcher from "WindowsBase.dll". Anyway, I'm grateful for your reply. I will close the issue, thanks~