/* * テキストの内容の変化を通知してきた。 */ private void EditContext_TextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs ev) { Debug.WriteLine("<<--- TextUpdating:({0},{1})->({2},{3}) [{4}] {5}", ev.Range.StartCaretPosition, ev.Range.EndCaretPosition, ev.NewSelection.StartCaretPosition, ev.NewSelection.EndCaretPosition, ev.Text, ev.Result ); // カーソルがあると文字の操作に邪魔なので、いったん取り除きます。 RemoveCursor(); // 以前の選択位置の文字を末尾から取り除いていきます。 for (int i = ev.Range.EndCaretPosition - 1; ev.Range.StartCaretPosition <= i; i--) { EditText.Inlines.RemoveAt(i); } // 新しいテキストを挿入します。 for (int i = 0; i < ev.Text.Length; i++) { // 1文字ごとにRunを作ります。 Run txt = new Run(); txt.Text = ev.Text.Substring(i, 1); // RunをTextBlockに挿入します。 EditText.Inlines.Insert(ev.Range.StartCaretPosition + i, txt); } // アプリ内で持っているテキストの選択位置を更新します。 Selection = ev.NewSelection; // カーソルを挿入します。 InsertCursor(); }
void EditContext_TextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs args) { if (!_compositionStarted) // Only update text when composition is started. { args.Result = CoreTextTextUpdatingResult.Failed; return; } CoreTextRange range = args.Range; string newText = args.Text; CoreTextRange newSelection = args.NewSelection; // Modify the internal text store. _inputBuffer = _inputBuffer.Substring(0, range.StartCaretPosition) + newText + _inputBuffer.Substring(Math.Min(_inputBuffer.Length, range.EndCaretPosition)); // You can set the proper font or direction for the updated text based on the language by checking // args.InputLanguage. We will not do that in this sample. // Modify the current selection. newSelection.EndCaretPosition = newSelection.StartCaretPosition; // Update the selection of the edit context. There is no need to notify the system // because the system itself changed the selection. SetSelection(newSelection); //var compStr = _inputBuffer; //compStr = compStr.Insert(_selection.StartCaretPosition, "|"); //Debug.WriteLine("composition text: {0}, cursor pos: {1}", (object)compStr, _selection.StartCaretPosition); OnTextComposition(_inputBuffer, _selection.StartCaretPosition); }
/* * テキストの内容の変化を通知してきた。 */ private void EditContext_TextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs ev) { Debug.WriteLine("<<--- TextUpdating:({0},{1})->({2},{3}) [{4}] {5} {6}", ev.Range.StartCaretPosition, ev.Range.EndCaretPosition, ev.NewSelection.StartCaretPosition, ev.NewSelection.EndCaretPosition, ev.Text, ev.Result, MeasureText(ev.Text, TextFormat) ); // テキストを変更して、変更情報をアンドゥ/リドゥのスタックにプッシュします。 PushUndoRedoStack(ev.Range.StartCaretPosition, ev.Range.EndCaretPosition, ev.Text, UndoStack); // 再描画します。 Win2DCanvas.Invalidate(); }
void EditContext_TextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs args) { CoreTextRange range = args.Range; string newText = args.Text; CoreTextRange newSelection = args.NewSelection; // Modify the internal text store. _text = _text.Substring(0, range.StartCaretPosition) + newText + _text.Substring(Math.Min(_text.Length, range.EndCaretPosition)); // You can set the proper font or direction for the updated text based on the language by checking // args.InputLanguage. We will not do that in this sample. // Modify the current selection. newSelection.EndCaretPosition = newSelection.StartCaretPosition; // Update the selection of the edit context. There is no need to notify the system // because the system itself changed the selection. SetSelection(newSelection); }
async void EditContext_TextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs args) { CoreTextRange range = args.Range; string newText = args.Text; CoreTextRange newSelection = args.NewSelection; var line = _content[CurrentLine].Text; //// Modify the internal text store. _content[CurrentLine].Text = line.Substring(0, range.StartCaretPosition) + newText + line.Substring(Math.Min(line.Length, range.EndCaretPosition)); if (_content[CurrentLine].Text.Trim().ToLower().StartsWith("todo:") && _content[CurrentLine] is not TodoTextItem) { var todoItem = new TodoTextItem() { Text = _content[CurrentLine].Text.Trim().Substring(5) }; // convert item to todo _content.RemoveAt(CurrentLine); _content.Insert(CurrentLine, todoItem); newSelection.StartCaretPosition = newSelection.EndCaretPosition = 0; // had to add this to make sure it works await Task.Delay(2); SetSelectionAndNotify(newSelection); } else { //// Modify the current selection. newSelection.EndCaretPosition = newSelection.StartCaretPosition; //// Update the selection of the edit context. There is no need to notify the system //// because the system itself changed the selection. SetSelection(newSelection); } }