简体   繁体   English

C#自定义字体值解释为字符串而不是单个值

[英]C# Custom Font Values interpreted as String instead of Single Value

I am using a Custom Font to display Icons in my WPF application. 我正在使用自定义字体在WPF应用程序中显示图标。 I wrote a little demo application to present me the icon alongside with the corresponding "Character". 我编写了一个演示应用程序,向我展示图标以及相应的“字符”。

Unfortunately, C# does not interpret the resulting string of my iterations as a single character, but as string. 不幸的是,C#并没有将我迭代的结果字符串解释为单个字符,而是字符串。

The code: 编码:

 private string _BaseString = "&#xE";
        public IconsPane()
        {
            InitializeComponent();
            InitIcons();
        }

        private void InitIcons()
        {

            string[] hex = new string[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

            string output = "";
            foreach (string a in hex)
            {
                foreach (string b in hex)
                {
                    foreach (string c in hex)
                    {
                        output = a + b + c;
                        CreateIcon(output);
                    }
                }

            }

        }

        private void CreateIcon(string output)
        {
            StackPanel st = new StackPanel();
            st.Margin = new Thickness(5);

            Button ILabel = new Button();
            ILabel.Style = (Style)FindResource("ContentButtonPlain");
            ILabel.Content = _BaseString + output + ";";

            TextBox IDesc = new TextBox();
            IDesc.Text = (string)ILabel.Content;
            IDesc.HorizontalAlignment = HorizontalAlignment.Center;

            st.Children.Add(ILabel);
            st.Children.Add(IDesc);

            IconDashboard.Children.Add(st);

        }

Output: 输出: 在此处输入图片说明

Instead I would like to have each Icon that is behind that string: 相反,我想让每个Icon都位于该字符串的后面:

Expected Output: 预期产量:

在此处输入图片说明

The screenshot of the expected output is taken from XAML code in the designer: 预期输出的屏幕截图来自设计器中的XAML代码:

<Button Margin="5 0 5 0"
                                                        Style="{StaticResource ContentButtonPlain}"
                                                        Content="&#xE3e2;" />

In this case, the icon is recognised in the correct way. 在这种情况下,将以正确的方式识别图标。

It seems that you want to create all symbols in range E000 to EFFF . 看来您想创建E000EFFF范围内的所有符号。 You could do it like this: 您可以这样做:

foreach (char symbolChar in Enumerable.Range(0xE000, 0x1000))
{
    var symbolString = new string(symbolChar, 1);

    // replace this by your actual code that creates UI elements
    st.Children.Add(new Label { Content = symbolString });
}

Besides that you may certainly better use an ItemsControl with an appropriate ItemTemplate to display the symbols, as shown here: https://stackoverflow.com/a/34156141/1136211 . 除此之外,您当然最好将ItemsControl与适当的ItemTemplate一起使用以显示符号,如下所示: https : //stackoverflow.com/a/34156141/1136211

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM