I have a contentEditable based contentEditable in a WebView in my Windows Store app. Some keyboard shortcuts and buttons may cause MessageDialog to open. When this dialog is rejected, the editor no longer has focus. I tried to adjust the focus as I know, and this will not work. Here is an example application.
MainPage.xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <WebView x:Name="Editor" Margin="200"></WebView> </Grid> <Page.BottomAppBar> <CommandBar x:Name="CommandBar_Editor" Visibility="Visible"> <AppBarButton Label="Debug" Icon="Setting"> <AppBarButton.Flyout> <MenuFlyout> <MenuFlyoutItem Text="show dialog, then focus" Click="MenuFlyoutItem_Click_1"/> </MenuFlyout> </AppBarButton.Flyout> </AppBarButton> </CommandBar> </Page.BottomAppBar> </Page>
MainPage.xaml.cs
public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); Editor.NavigateToString("<script type='text/javascript'>function focus_please(){ document.getElementById('editor').focus(); }</script><div id='editor' contentEditable='true'>It was the best of times, it was the worst of times</div>"); } private async void MenuFlyoutItem_Click_1(object sender, RoutedEventArgs e) { MessageDialog dialog = new MessageDialog("this should set focus to editor on close", "test"); UICommand okCommand = new UICommand("OK"); dialog.Commands.Add(okCommand); IUICommand response = await dialog.ShowAsync(); if (response == okCommand) { Editor.Focus(FocusState.Programmatic);
It seems to me that the WebView focused, but not the HTML content inside
Update:
My example has been updated to add Brian code from the answer below, but it still doesn't work.
Note After rejecting MessageDialog, if I press Tab twice, the editor becomes active again.
Update 2:
Brian's answer below works when using the touch screen for navigation. However, when using the mouse and keyboard, the contentEditable element contentEditable not focus. I put generosity on this to find a solution that allows you to focus on the element either when using the touch screen, or in combination with a mouse / keyboard.
source share