如何使UWP TextBlock可以手写编辑?

时间:2019-10-29 17:00:53

标签: c# xaml uwp handwriting-recognition handwriting

我目前正在显示TextBlock,其中的文本可以由用户使用游戏手柄,鼠标和键盘以各种方式进行操作。

我希望用户能够在此TextBlock上写文本,然后该书写将替换TextBlock的文本。

该应用程序的工作方式如下:

<TextBlock x:Name="TheTextBlock" Text="Sample Text" />

后面的代码是这样的:

private void Page_KeyDown(object sender, KeyEventArgs args)
{
    if (args.Key = VirtualKey.LeftButton)
        TheTextBlock.Text = "Left Button Pressed";
}

因此,在写东西时,用InkCanvas<TextBox IsHandwritingViewEnabled="False" />,文字应该出现在TextBlock中,墨水应该消失。

您该怎么做? InkCanvas上方的TextBlock是否可以在将笔按在画布上时清除文本?一个看不见的手写文字框?

1 个答案:

答案 0 :(得分:1)

  

您该怎么做?在TextBlock顶部的InkCanvas是否可以在将笔按在画布上时清除文本?一个看不见的手写文字框?

从您的描述来看,您似乎想要用手写板识别文本,然后将其显示在TextBlock中。请参阅此document。我提供了您可以参考的示例。

Xaml

<Grid>
    <TextBlock x:Name="TheTextBlock" Text="Sample Text"/>
    <InkCanvas x:Name="TheInkCanvas" />
</Grid>

隐藏代码

InkAnalyzer inkAnalyzer;
DispatcherTimer recoTimer;
public MainPage()
{
    this.InitializeComponent();
    TheInkCanvas.InkPresenter.InputDeviceTypes =
       Windows.UI.Core.CoreInputDeviceTypes.Mouse |
       Windows.UI.Core.CoreInputDeviceTypes.Pen;

    TheInkCanvas.InkPresenter.StrokesCollected += inkCanvas_StrokesCollected;
    // StrokeStarted is fired when ink input is first detected.
    TheInkCanvas.InkPresenter.StrokeInput.StrokeStarted +=
        inkCanvas_StrokeStarted;

    inkAnalyzer = new InkAnalyzer();

    // Timer to manage dynamic recognition.
    recoTimer = new DispatcherTimer();
    recoTimer.Interval = TimeSpan.FromSeconds(1);
    recoTimer.Tick += recoTimer_TickAsync;
}

private async void recoTimer_TickAsync(object sender, object e)
{
    recoTimer.Stop();
    if (!inkAnalyzer.IsAnalyzing)
    {
        InkAnalysisResult result = await inkAnalyzer.AnalyzeAsync();

        // Have ink strokes on the canvas changed?
        if (result.Status == InkAnalysisStatus.Updated)
        {
            // Find all strokes that are recognized as handwriting and 
            // create a corresponding ink analysis InkWord node.
            var inkwordNodes =
                inkAnalyzer.AnalysisRoot.FindNodes(
                    InkAnalysisNodeKind.InkWord);

            // Iterate through each InkWord node.
            // Display the primary recognized text (for this example, 
            // we ignore alternatives), and then delete the 
            // ink analysis data and recognized strokes.
            foreach (InkAnalysisInkWord node in inkwordNodes)
            {
                string recognizedText = node.RecognizedText;
                // Display the recognition candidates.
                TheTextBlock.Text = recognizedText;

                foreach (var strokeId in node.GetStrokeIds())
                {
                    var stroke =
                        TheInkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                    stroke.Selected = true;
                }
                inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds());
            }
            TheInkCanvas.InkPresenter.StrokeContainer.DeleteSelected();
        }
    }
    else
    {
        // Ink analyzer is busy. Wait a while and try again.
        recoTimer.Start();
    }
}

private void inkCanvas_StrokeStarted(InkStrokeInput sender, PointerEventArgs args)
{
    recoTimer.Stop();
}

private void inkCanvas_StrokesCollected(InkPresenter sender, InkStrokesCollectedEventArgs args)
{
    recoTimer.Stop();
    foreach (var stroke in args.Strokes)
    {
        inkAnalyzer.AddDataForStroke(stroke);
        inkAnalyzer.SetStrokeDataKind(stroke.Id, InkAnalysisStrokeKind.Writing);
    }
    recoTimer.Start();
}