C# (WinRT)如何获取文本框插入符号索引?
C# (WinRT)如何获取文本框插入符号索引?,c#,xaml,windows-runtime,windows-phone-8.1,winrt-xaml,C#,Xaml,Windows Runtime,Windows Phone 8.1,Winrt Xaml,在Windows应用商店应用程序(WP 8.1)中获取文本框的插入符号索引时遇到一些问题。 按下按钮时,我需要在文本中插入特定符号。 我试过这个: text.Text = text.Text.Insert(text.SelectionStart, "~"); 但此代码将符号插入到文本的开头,而不是插入插入符号所在的位置 更新 我更新了我的代码感谢。但现在我遇到了另一个问题:我正在构建HMTL编辑器应用程序,所以我的默认文本块是: <!DOCTYPE html>\r\n<
在Windows应用商店应用程序(WP 8.1)中获取文本框的插入符号索引时遇到一些问题。
按下按钮时,我需要在文本中插入特定符号。 我试过这个:
text.Text = text.Text.Insert(text.SelectionStart, "~");
但此代码将符号插入到文本的开头,而不是插入插入符号所在的位置 更新 我更新了我的代码感谢。但现在我遇到了另一个问题:我正在构建HMTL编辑器应用程序,所以我的默认文本块是:
<!DOCTYPE html>\r\n<html>\r\n<head>\r\n</head>\r\n<body>\r\n</body>\r\n</html>
那么如何解决这个问题呢
我猜新行字符会制造麻烦。对于您的第一期,我假设您在访问
选择开始之前更改了文本框。Text
。设置text.text
时,text.SelectionStart
将重置为0
关于你的第二个问题与新的线路
你可以说你所观察到的是故意的<代码>选择开始
将一个“\r\n”作为一个字符,原因如下(请参见备注部分)。另一方面,方法string.Insert
不关心该方面,并将“\r\n”计为两个字符
您需要稍微更改代码。不能使用SelectionStart
的值作为插入位置。您需要根据SelectionStart
的这种行为计算插入位置
下面是一个详细的代码示例,其中包含一个可能的解决方案
// normalizedText will allow you to separate the text before
// the caret even without knowing how many new line characters you have.
string normalizedText = text.Text.Replace("\r\n", "\n");
string textBeforeCaret = normalizedText.Substring(0, text.SelectionStart);
// Now that you have the text before the caret you can count the new lines.
// that need to be accounted for.
int newLineCount = textBeforeCaret.Count(c => c == '\n');
// Knowing the new lines you can calculate the insert position.
int insertPosition = text.SelectionStart + newLineCount;
text.Text = text.Text.Insert(insertPosition, "~");
此外,您还应确保SelectionStart不会表现出与“\r\n”旁边的其他组合类似的行为。如果需要,您需要更新上面的代码。对于第一期,我假设您在访问
选择开始之前更改了文本框。Text
。设置text.text
时,text.SelectionStart
将重置为0
关于你的第二个问题与新的线路
你可以说你所观察到的是故意的<代码>选择开始
将一个“\r\n”作为一个字符,原因如下(请参见备注部分)。另一方面,方法string.Insert
不关心该方面,并将“\r\n”计为两个字符
您需要稍微更改代码。不能使用SelectionStart
的值作为插入位置。您需要根据SelectionStart
的这种行为计算插入位置
下面是一个详细的代码示例,其中包含一个可能的解决方案
// normalizedText will allow you to separate the text before
// the caret even without knowing how many new line characters you have.
string normalizedText = text.Text.Replace("\r\n", "\n");
string textBeforeCaret = normalizedText.Substring(0, text.SelectionStart);
// Now that you have the text before the caret you can count the new lines.
// that need to be accounted for.
int newLineCount = textBeforeCaret.Count(c => c == '\n');
// Knowing the new lines you can calculate the insert position.
int insertPosition = text.SelectionStart + newLineCount;
text.Text = text.Text.Insert(insertPosition, "~");
此外,您还应确保SelectionStart不会表现出与“\r\n”旁边的其他组合类似的行为。如果需要,您需要更新上面的代码。对于第一期,我假设您在访问
选择开始之前更改了文本框。Text
。设置text.text
时,text.SelectionStart
将重置为0
关于你的第二个问题与新的线路
你可以说你所观察到的是故意的<代码>选择开始
将一个“\r\n”作为一个字符,原因如下(请参见备注部分)。另一方面,方法string.Insert
不关心该方面,并将“\r\n”计为两个字符
您需要稍微更改代码。不能使用SelectionStart
的值作为插入位置。您需要根据SelectionStart
的这种行为计算插入位置
下面是一个详细的代码示例,其中包含一个可能的解决方案
// normalizedText will allow you to separate the text before
// the caret even without knowing how many new line characters you have.
string normalizedText = text.Text.Replace("\r\n", "\n");
string textBeforeCaret = normalizedText.Substring(0, text.SelectionStart);
// Now that you have the text before the caret you can count the new lines.
// that need to be accounted for.
int newLineCount = textBeforeCaret.Count(c => c == '\n');
// Knowing the new lines you can calculate the insert position.
int insertPosition = text.SelectionStart + newLineCount;
text.Text = text.Text.Insert(insertPosition, "~");
此外,您还应确保SelectionStart不会表现出与“\r\n”旁边的其他组合类似的行为。如果需要,您需要更新上面的代码。对于第一期,我假设您在访问
选择开始之前更改了文本框。Text
。设置text.text
时,text.SelectionStart
将重置为0
关于你的第二个问题与新的线路
你可以说你所观察到的是故意的<代码>选择开始
将一个“\r\n”作为一个字符,原因如下(请参见备注部分)。另一方面,方法string.Insert
不关心该方面,并将“\r\n”计为两个字符
您需要稍微更改代码。不能使用SelectionStart
的值作为插入位置。您需要根据SelectionStart
的这种行为计算插入位置
下面是一个详细的代码示例,其中包含一个可能的解决方案
// normalizedText will allow you to separate the text before
// the caret even without knowing how many new line characters you have.
string normalizedText = text.Text.Replace("\r\n", "\n");
string textBeforeCaret = normalizedText.Substring(0, text.SelectionStart);
// Now that you have the text before the caret you can count the new lines.
// that need to be accounted for.
int newLineCount = textBeforeCaret.Count(c => c == '\n');
// Knowing the new lines you can calculate the insert position.
int insertPosition = text.SelectionStart + newLineCount;
text.Text = text.Text.Insert(insertPosition, "~");
此外,您还应确保SelectionStart不会表现出与“\r\n”旁边的其他组合类似的行为。如果需要,您需要更新上面的代码。您的代码可以正常工作(我刚刚检查过)。在尝试访问Text.SelectionStart之前是否有可能更改文本?因为如果您这样做,当您设置text.text时,text.SelectionStart将重置为0。@Ladi请检查更新您的代码工作(我刚刚检查过)。在尝试访问Text.SelectionStart之前是否有可能更改文本?因为如果您这样做,当您设置text.text时,text.SelectionStart将重置为0。@Ladi请检查更新您的代码工作(我刚刚检查过)。在尝试访问Text.SelectionStart之前是否有可能更改文本?因为如果您这样做,当您设置text.text时,text.SelectionStart将重置为0。@Ladi请检查更新您的代码工作(我刚刚检查过)。在尝试访问Text.SelectionStart之前是否有可能更改文本?因为如果您这样做,当您设置text.text时,text.SelectionStart将重置为0。@Ladi请查看更新,谢谢,伙计!那真的很有帮助。谢谢你,伙计!那真的很有帮助。谢谢你,伙计!那真的很有帮助。谢谢你,伙计!这真的很有帮助。
// normalizedText will allow you to separate the text before
// the caret even without knowing how many new line characters you have.
string normalizedText = text.Text.Replace("\r\n", "\n");
string textBeforeCaret = normalizedText.Substring(0, text.SelectionStart);
// Now that you have the text before the caret you can count the new lines.
// that need to be accounted for.
int newLineCount = textBeforeCaret.Count(c => c == '\n');
// Knowing the new lines you can calculate the insert position.
int insertPosition = text.SelectionStart + newLineCount;
text.Text = text.Text.Insert(insertPosition, "~");