using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.ApplicationModel; using Windows.Storage; using Windows.UI; using Windows.UI.Text; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; namespace JpInkRecognizer.ViewModel { public class HandWriterViewModel: BaseViewModel { public RichEditBox NameInputREBControl { get; set; } public InkCanvas InkCanvasControl { get; set; } public async void NameInput_Loaded(object sender, RoutedEventArgs e) { this.allowProcess = WinAppConstant.StrProcessAllow; this.LoadInit(); string textStr = this.GetTextFromRichEditBox(this.NameInputREBControl); this.NameInputREBControl.Document.Selection.StartPosition = textStr.Length; await this.ChangeTextColor(this.NameInputREBControl, textStr, Colors.Red); this.NameInputREBControl.Focus(FocusState.Programmatic); } private void LoadInit() { // Init inkcanvas this.InialInkCanvas(this.InkCanvasControl, this.NameInputREBControl); // Collect strokes this.InkCanvasControl.InkPresenter.StrokesCollected += this.InkPresenter_StrokesCollected; // Event Register this.NameInputREBControl.AddHandler(UIElement.PointerPressedEvent, new PointerEventHandler(TextBox_OnPointerPressed), true); } private string GetTextFromRichEditBox(RichEditBox richEditBox) { string temp; richEditBox.Document.GetText(Windows.UI.Text.TextGetOptions.None, out temp); var range = richEditBox.Document.GetRange(0, temp.Length - 1); string textStr; range.GetText(Windows.UI.Text.TextGetOptions.None, out textStr); return textStr; } public async void BtnSpace_Click(object sender, RoutedEventArgs e) { if (this.allowProcess == WinAppConstant.StrProcessAllow) { this.allowProcess = WinAppConstant.StrProcessDenied; this.Blank(); string textStr = this.GetTextFromRichEditBox(this.NameInputREBControl); await this.ChangeTextColor(this.NameInputREBControl, textStr, Colors.Red); this.allowProcess = WinAppConstant.StrProcessAllow; } else { return; } } public async void BtnDelete_Click(object sender, RoutedEventArgs e) { if (this.allowProcess == WinAppConstant.StrProcessAllow) { this.allowProcess = WinAppConstant.StrProcessDenied; this.DeleteOneWord(); string textStr = this.GetTextFromRichEditBox(this.NameInputREBControl); await this.ChangeTextColor(this.NameInputREBControl, textStr, Colors.Red); this.allowProcess = WinAppConstant.StrProcessAllow; } else { return; } } public void BtnDeleteAll_Click(object sender, RoutedEventArgs e) { if (this.allowProcess == WinAppConstant.StrProcessAllow) { this.allowProcess = WinAppConstant.StrProcessDenied; this.DeleteAllWord(); this.allowProcess = WinAppConstant.StrProcessAllow; } else { return; } } public void Button_Click_Right(object sender, RoutedEventArgs e) { if (this.allowProcess == WinAppConstant.StrProcessAllow) { this.allowProcess = WinAppConstant.StrProcessDenied; string textStr; this.NameInputREBControl.Document.GetText(TextGetOptions.None, out textStr); if (this.NameInputREBControl.Document.Selection.StartPosition < textStr.Length - 1) { this.NameInputREBControl.Focus(FocusState.Programmatic); this.NameInputREBControl.Document.Selection.StartPosition = this.NameInputREBControl.Document.Selection.StartPosition + 1; this.NameInputREBControl.Document.Selection.SetRange(this.NameInputREBControl.Document.Selection.StartPosition, this.NameInputREBControl.Document.Selection.StartPosition); } else { this.NameInputREBControl.Focus(FocusState.Programmatic); this.NameInputREBControl.Document.Selection.StartPosition = textStr.Length; this.NameInputREBControl.Document.Selection.SetRange(textStr.Length, textStr.Length); } this.ClearCanvas(); this.allowProcess = WinAppConstant.StrProcessAllow; } else { return; } } public void Button_Click_Left(object sender, RoutedEventArgs e) { if (this.allowProcess == WinAppConstant.StrProcessAllow) { this.allowProcess = WinAppConstant.StrProcessDenied; if (this.NameInputREBControl.Document.Selection.StartPosition > 0) { this.NameInputREBControl.Focus(FocusState.Programmatic); this.NameInputREBControl.Document.Selection.StartPosition = this.NameInputREBControl.Document.Selection.StartPosition - 1; this.NameInputREBControl.Document.Selection.SetRange(this.NameInputREBControl.Document.Selection.StartPosition, this.NameInputREBControl.Document.Selection.StartPosition); } else { this.NameInputREBControl.Focus(FocusState.Programmatic); this.NameInputREBControl.Document.Selection.StartPosition = 0; this.NameInputREBControl.Document.Selection.SetRange(0, 0); } this.ClearCanvas(); this.allowProcess = WinAppConstant.StrProcessAllow; } else { return; } } } }