我正在努力在 WPF 中将索引显示为上标。一些字符在不应该出现时作为上标出现

I'm struggling to display indices as superscript in WPF. Some characters coming out as superscript when they shouldn't

我正在尝试构建一个 WPF 应用程序来协助进行一些数学修订,但我在将索引值显示为上标时遇到了问题。我正在尝试将生成的问题字符串格式化为代码隐藏的所需输出。我最初试图将生成的问题简单地显示为文本块中的字符串。 (在输入字符串中每个值的开头和结尾用“#”标记索引值。)

        private char ConvertSingleToSuperscript(char input)
    {
        switch (input)
        {
            case '0':
                return '⁰';
            case '1':
                return '¹';
            case '2':
                return '²';
            case '3':
                return '³';
            case '4':
                return '⁴';
            case '5':
                return '⁵';
            case '6':
                return '6';
            case '7':
                return '⁷';
            case '8':
                return '⁸';
            case '9':
                return '⁹';
            case '+':
                return '⁺';
            case '-':
                return '⁻';
            case '=':
                return '⁼';
            case '/':
                return char.Parse(char.ConvertFromUtf32(0x2044));
            default:
                throw new Exception("Incorrect character entered as superscript, change entered indice value.");
        }
    }

 public string ProcessSuperscript(string input)
    {
        char[] tempArray = input.ToCharArray();
        string outputString = "";
        bool processingIndice = false;
        foreach(char c in tempArray)
        {
            if(c == '#')
            {
                processingIndice = !processingIndice;
            }
            if (processingIndice)
            {
                outputString += ConvertSingleToSuperscript(c);
            }
            else
            {
                outputString += c;
            }
        }
        return outputString;
    }

使用这个,或者让我的 'ConvertSingleToSuperScript' 方法 return 通过相关上标 unicode 生成的字符值总是 return 字符串,其中 1、2 和 3 显示为上标项其余所有 return 文本的大小与字符串中其余 'non-indice' 值的大小相同。

最近我尝试使用文本块内联运行来获得相同的结果:

        private void FormatQuestionSuperScriptDisplay(TextBlock displayBlock, string variableText)
    {
        char[] tempArray = variableText.ToCharArray();
        string processString = "";
        bool processingIndice = false;
        foreach (char c in tempArray)
        {
            if (c == '#')
            {
                if (!processingIndice)
                {
                    displayBlock.Inlines.Add(processString);
                    Console.WriteLine(processString + " as normal");
                }
                else
                {
                    displayBlock.Inlines.Add(new Run(processString) { BaselineAlignment = BaselineAlignment.Superscript });
                    Console.WriteLine(processString + " as indice.");
                }
                processString = "";
                processingIndice = !processingIndice;
                continue;
            }
            processString += c;
        }
        displayBlock.Inlines.Add(processString);
    }
}

我一定是在这里的某个地方搞砸了,现在我的输出将如下图所示来自字符串:“abcdefghijklmnopqrstuvwxyz 12#32# 15#4589# test”

For those unable to see the image the characters for 'i' and 'n' are displayed as superscript characters

我还有一个问题是运行一次通过Inlines.Add()方法的所有数值也被处理为上标字符,如何处理它们(我不确定正确的术语是什么,所以我会说)以与字符串的其余部分匹配的正常大小?

任何关于我应该阅读的内容的帮助或指示都会非常有帮助。谢谢你的时间。

这对我有用。 检查函数 ProcessSuperscript.

代码隐藏

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace SuperScript
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            FormatQuestionSuperScriptDisplay(tbText, "abcdefghijklmnopqrstuvwxyz 12#32# 15#4589# test");
        }

        private char ConvertSingleToSuperscript(char input)
        {
            switch (input)
            {
                case '0':
                    return '⁰';
                case '1':
                    return '¹';
                case '2':
                    return '²';
                case '3':
                    return '³';
                case '4':
                    return '⁴';
                case '5':
                    return '⁵';
                case '6':
                    return '6';
                case '7':
                    return '⁷';
                case '8':
                    return '⁸';
                case '9':
                    return '⁹';
                case '+':
                    return '⁺';
                case '-':
                    return '⁻';
                case '=':
                    return '⁼';
                case '/':
                    return char.Parse(char.ConvertFromUtf32(0x2044));
                default:
                    throw new Exception("Incorrect character entered as superscript, change entered indice value.");
            }
        }

        public string ProcessSuperscript(string input)
        {
            char[] tempArray = input.ToCharArray();
            string outputString = "";
            bool processingIndice = false;
            foreach (char c in tempArray)
            {
                if (c == '#')
                {
                    processingIndice = !processingIndice;
                    continue; //<- This code added.
                }
                if (processingIndice)
                {
                    outputString += ConvertSingleToSuperscript(c);
                }
                else
                {
                    outputString += c;
                }
            }
            return outputString;
        }

        private void FormatQuestionSuperScriptDisplay(TextBlock displayBlock, string variableText)
        {
            char[] tempArray = variableText.ToCharArray();
            string processString = "";
            bool processingIndice = false;
            foreach (char c in tempArray)
            {
                if (c == '#')
                {
                    if (!processingIndice)
                    {
                        displayBlock.Inlines.Add(processString);
                        Console.WriteLine(processString + " as normal");
                    }
                    else
                    {
                        displayBlock.Inlines.Add(new Run(processString) { BaselineAlignment = BaselineAlignment.Superscript });
                        Console.WriteLine(processString + " as indice.");
                    }
                    processString = "";
                    processingIndice = !processingIndice;
                    continue;
                }
                processString += c;
            }
            displayBlock.Inlines.Add(processString);
        }
    }
}