簡體   English   中英

如何使 UWP TextBlock 可通過手寫進行編輯?

[英]How do you make an UWP TextBlock editable with 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中並且墨水應該消失。

你怎么能做到這一點? TextBlock頂部的InkCanvas將在將筆按在 canvas 上時清除文本? 一個看不見的手寫文本框?

你怎么能做到這一點? TextBlock 頂部的 InkCanvas 將在將筆按在 canvas 上時清除文本? 一個看不見的手寫文本框?

根據您的描述,您似乎想制作手寫板來識別文本,然后將其顯示在TextBlock中。 請參考本文檔 我已經制作了您可以參考的樣本。

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM