private static List <Run> GetRuns(FormatParameters formatParameters, string input) { List <Run> result = new List <Run>(); //处理输入格式化参数 if (formatParameters != null && formatParameters.Count > 0) { Match match = null; do { match = Regex.Match(input, @"(.*?)({(\d+)})"); if (match.Groups.Count >= 4) { var source = match.Groups[0].Value; var text = match.Groups[1].Value; //插入正文 result.Add(new Run() { Text = text }); //插入format var templateIndexStr = match.Groups[3].Value; int.TryParse(templateIndexStr, out int templateIndex); if (templateIndex < formatParameters.Count) { var item = formatParameters[templateIndex]; if (item is Run) { result.Add(item as Run); } else if (item is TextBlock) { var textBlock = item as TextBlock; result.Add(new Run() { Text = textBlock.Text }); foreach (Run inlineItem in textBlock.Inlines) { result.Add(inlineItem); } } else { result.Add(new Run() { Text = item.ToString() }); } } //删除已经处理过的文字input input = input.Remove(0, source.Length); } } while (match != null && match.Success); if (!string.IsNullOrEmpty(input)) { result.Add(new Run() { Text = input }); } } else { result.Add(new Run() { Text = input }); } return(result); }
public static void SetParameters(DependencyObject obj, FormatParameters value) { obj.SetValue(ParametersProperty, value); }