我的項目是一個Windows 10通用應用程序!c#rtb:Bolden inline覆蓋另一個bolden內嵌
我差不多有兩個月的問題,當我在一個rtb中加粗兩個單詞時,第二個覆蓋第一個粗體內聯。
例子:
我想大膽:
Hello; Bye
從RTB文字:
Hello and Bye
現在我用正則表達式搜索,天氣療法是 「你好」/ 「再見」,在RTB。 每次在rtb中都會出現「Hello」,我會在與「hello」站點相同的位置插入bolden內嵌文本「Hello」。
之後,我做了與「再見」相同。
我的代碼:
string text = run.Text; -> "Hello and Bye"
MatchCollection mc = Regex.Matches(text, "Hello", RegexOptions.Multiline);
int i = 0;
var bold = new Bold();
int iIn = 0;
int iLe = 0;
p.Inlines.Clear(); -> p = Paragraph from rtb
foreach (Match match in mc)
{
p.Inlines.Add(new Run { Text = text.Substring(i, match.Index - i) });
bold.Inlines.Add(new Run { Text = text.Substring(match.Index, match.Length) });
p.Inlines.Add(bold);
i = match.Index + match.Length;
if (i < text.Length)
{
p.Inlines.Add(new Run { Text = text.Substring(i) });
}
}
其次是與再見相同的代碼。
現在的問題是,我清除首次大膽在線(你好),而插入第二個大膽的內聯(再見)。
有沒有人知道在rtb中用粗體表示一個特定詞的替代方案還是改善代碼的建議?我幾乎tryed一切,但沒有真正的工作...
你叫'p.Inlines.Clear()'一次或兩次? –
@MatthewStrawbridge我把它叫做bevore,在hello和bye中加入bolden inline。 –
@ehh這聽起來不錯,但我不能將int轉換爲Windows.UI.Xaml.Documents.TextPointer(註釋回答) –