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