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

CreateCoreWebView2ControllerAsync can't get a await return #3617

Closed
1CDE683405CAC00EAD7242DCD02BC0AB opened this issue Jul 4, 2023 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@1CDE683405CAC00EAD7242DCD02BC0AB
Copy link

1CDE683405CAC00EAD7242DCD02BC0AB commented Jul 4, 2023

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
Copy link
Contributor

novac42 commented Jul 6, 2023

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.

@novac42 novac42 added bug Something isn't working and removed question labels Jul 18, 2023
@yunate
Copy link
Contributor

yunate commented Jul 18, 2023

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
Copy link
Author

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~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants