我目前正在显示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
是否可以在将笔按在画布上时清除文本?一个看不见的手写文字框?
答案 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();
}