如何使 UWP TextBlock 可通过手写进行编辑?
Posted
技术标签:
【中文标题】如何使 UWP TextBlock 可通过手写进行编辑?【英文标题】:How do you make an UWP TextBlock editable with handwriting? 【发布时间】:2020-02-24 23:23:22 【问题描述】:我目前正在展示一个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】:你怎么能做到这一点? 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();
【讨论】:
以上是关于如何使 UWP TextBlock 可通过手写进行编辑?的主要内容,如果未能解决你的问题,请参考以下文章
(多)在 UWP 或 WinUI3 中将列表绑定到 TextBlock