Search code examples
c#uwpfocusuwp-xamltext-cursor

How to make UWP not taking focus, and keep text cursor outside?


I have a UWP app with handwriting feature, testing on a laptop with touch screen. After finishing handwriting, the recognized text will be store in a textblock temporally. And then what I expect to do is, the text in the textblock should be transmitted to the position of text cursor, which might be in notpad, search box of browser, chat room, etc.

However, when I perform handwrite on my app, focus will be taken into the app, and the text cursor will disappear. In general, focus will be always taken immediately once I touch my app. This is my critical issue to be resolved.

In WPF, I can achieve my objective by setting WS_EX_NOACTIVATE into HWND, as this post. The scenario is the same. But In UWP, the method cannot be applied, since the concept of HWND is not supported in UWP.

(By the way, the reason I need to build UWP is Windows.UI.Xaml.Controls.HandwritingView, which is not supported in WPF.)

Besides, I have also tried some properties of UserControls. I have set AllowFocusOnInteraction="False" to all related controls in my project: Page-Grid-Textbox-HandwritingView, and have tried some ideas in the post, even if the scenario is different.
Unforfunately, all of them did not work. The focus is still taken once I touch my app.

I have been trapped here several days. Thanks for any idea in advance.

Below is my code sample:

<Page
x:Class="UWPtest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWPtest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Width="1440"
Height="1300"
AllowFocusOnInteraction="False"  
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <TextBox x:Name="tbHandWriteView" HorizontalAlignment="Center" Margin="0,0,0,0" FontSize="60" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="1300" Height="1200" FontFamily="Segoe UI"/>
</Grid>
    public MainPage()
    {
        this.InitializeComponent();

        tbHandWriteView.IsHandwritingViewEnabled = true;
        tbHandWriteView.HandwritingView.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Touch | Windows.UI.Core.CoreInputDeviceTypes.Pen;
        tbHandWriteView.HandwritingView.PlacementAlignment = HandwritingPanelPlacementAlignment.TopLeft;
        tbHandWriteView.HandwritingView.Width = 1000;
        tbHandWriteView.HandwritingView.Height = 1000;
        tbHandWriteView.HandwritingView.Visibility = Visibility.Visible;


        this.AllowFocusOnInteraction = false;
        tbHandWriteView.AllowFocusOnInteraction = false;
        tbHandWriteView.HandwritingView.AllowFocusOnInteraction = false;

        tbHandWriteView.HandwritingView.TryOpen();
    }

Below is the problem I met:

  1. I touch the notepad, the text cursor will be in notepad.
  2. I touch my UWP app for handwriting.
  3. The focus was taken by app, and the text cursor in notepad disappeared.

video recorded here


Solution

  • I understand what you are trying to achieve. But this is expected behavior for UWP apps. When you click on UWP apps, the UWP app will gain focus. There is no API could do what you want in UWP apps.