我正在使用Visual Studio中的C#控制台项目,我想更改StringBuilder
对象的一部分的颜色。我无法使用WriteLine
立即将该部分代码写入控制台,因为它是较大段落的一部分。因此,Is it possible to write to the console in colour in .NET?中提供的解决方案(在WriteLine
语句之间使用ConsoleColor.Cyan
语句)不适用。
这是我目前的方法:
internal void ShowItems(StringBuilder builder, Player player)
{
foreach (Item item in Program.items.Values)
{
if (item.CurrentLocation == player.CurrentLocation.Name && item.CurrentLocation == item.InitialLocation)
{
builder.Append(item.InitialText);
}
else if (item.CurrentLocation == player.CurrentLocation.Name && item.CurrentLocation != item.InitialLocation)
{
builder.Append($" You see the {item.DisplayName.ToUpper()} here.");
}
}
}
我希望部分item.InitialText
以另一种颜色出现,比如洋红色。此颜色与文本的其余部分不同。我可以将item.InitialText
拆分成我想要的字符串,但是如何使其中一个字符串变成不同的颜色呢?
我查看了几个资源,但它们似乎都是用于富文本框或其他图形界面,而不是控制台输出。