WebView.Focus not working

ansalc 436 Reputation points
2020-04-26T10:34:54.18+00:00

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?

Universal Windows Platform (UWP)
{count} vote

1 answer

Sort by: Most helpful
  1. ansalc 436 Reputation points
    2020-04-28T07:50:00.54+00:00

    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"/>