question

ansalc-1556 avatar image
1 Vote"
ansalc-1556 asked ·

WebView.Focus not working

the following line works well when I debug the app (press F5):

WebView2.Focus(FocusState.Programmatic)

WebView2 gains focus, it comes to the foreground

However, when I run the app from the Start menu, or from its shortcut, WebView does not come to the foreground

Why is this?

How can it be fixed?

windows-uwp
· 3
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have some questions about your issue. You are developing a UWP app that contains a WebView, right?

I'm a little confused about what you said- 'WebView2 gains focus, it comes to the foreground'. Could you please explain more about this expected behavior?

Besides, may I know why do you want to make your WebView focused?

0 Votes 0 ·

Yes, I am developing a UWP app that contains a WebView.


I need to bring to the foreground the WebView because the app runs for long in the background but when it reaches a certain point I need to see the WebView.


I also need to input some key strokes programmatically with inputInjector.InjectKeyboardInput


For both reasons, I need to make the WebView focused.


Again, it works fine when I run the application from Visual Studio, but when I run it from the Start menu or shortcut the object that I call WebView2 does not come into focus. I do not see the website and the keystroks are not injected in the website, but in the foreground window at the time.


0 Votes 0 ·

I'm a little confused. I made a simple test, the WebView is focused correctly using the code. For example, it could be scrolled up and down by up and down key. If this is not what you want, could you please tell me what's the expected behavior specifically?

It also might be better if you could share the code snippet about your XAML code that contains the WebView and the code-behind. This could help me to understand your scenario better.

0 Votes 0 ·

1 Answer

ansalc-1556 avatar image
0 Votes"
ansalc-1556 answered ·

WebView2.Focus(FocusState.Programmatic) works as expected if I launch the program from Visual Studio (if I press F5)

However, if I launch it from the Start menu and there is a different window in the foreground when the line above gets executed, WebView2 does not come to the foreground.

Did you try this? Did you try launching the program from the Start menu and bring to the foreground a different window before the Webview.focus is executed?

My code and xaml are very simple:



 Private Async Sub WebView2_DOMContentLoaded(sender As WebView, args As WebViewDOMContentLoadedEventArgs) Handles WebView2.DOMContentLoaded


     WebView2.Focus(FocusState.Programmatic)


     Await Task.Delay(5000).ConfigureAwait(False)

...



<Grid x:Name="MyGrid">
<WebView x:Name="WebView1" Margin="0,10,365,210" Visibility="Visible"/>
<WebView x:Name="WebView2" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto"/>

· 11 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What do you mean by a different window in the foreground ? A new WebView control in the same parent control or another app?

0 Votes 0 ·

Another app.


0 Votes 0 ·

It is not possible to bring the UWP app to the foreground programmatically when the UWP app is not in the foreground.

0 Votes 0 ·

Really? That's a deal breaker.

Any other way to do it?

Why does it work when debugging?


0 Votes 0 ·

There is no other way to do that. This is by design. When you launch and debug the app in Visual Studio, Visual Studio will automatically bring the target app to the foreground.

I understand your feeling about this. You could submit a feature request in the Feedback Hub. You could find the Feedback Hub in the Start menu.

0 Votes 0 ·

When launching and debugging the app in Visual Studio, Visual Studio does bring the target to the foreground. That is fine (and it happens as well when launching the app normally) but then, if the user brings some other app to the foreground, when debugging the focus works programmatically but not when launching the app normally. That does not make sense it is by design. Why the different behaviour? If it were by design it would render debugging useless in this case, and that it is hard to believe it is by design


0 Votes 0 ·

UWP apps are different from Win32 apps. Win32 apps could bring itself to the foreground but UWP can't do that and that win32 API is not supported in UWP apps like findwindows. And another thing that needs to be clarified is that the Webview gets focused doesn't mean the UWP app gets focused. The UWP app will not come to the foreground when the app is not in the foreground. WebView is a control inside the UWP app. WebView gets focused only means this control inside the UWP app is focused.

0 Votes 0 ·

Yes, it makes sense that WebView focused only means that the WebView control inside the UWP app is focused.


It is not clearly explained in the documentation, so it was worth a try.


In any case, if UWP apps cannot bring themselves to the foreground it is certainly a missing feature. I have submitted a feature request in the Feedback Hub


0 Votes 0 ·

Thank you for giving to giving advice!

0 Votes 0 ·
Show more comments