示例#1
0
        public static TextBlock GetFormattedText(string str, int size = 20, Dictionary <string, object> dict = null, bool iswhite = false, bool scroll = true)
        {
            int       i;
            double    sub       = size * 0.65;
            double    f         = size * 1.5;
            string    plaintext = "";
            TextBlock t         = new TextBlock()
            {
                FontSize = size, TextWrapping = TextWrapping.Wrap
            };

            for (; str != "";)
            {
                i = 0;
                if (str.StartsWith("<LineBreak/>"))
                {
                    t.Inlines.Add(plaintext);
                    plaintext = "";
                    t.Inlines.Add(new LineBreak());
                    i += 12;
                }
                else if (str.StartsWith("<sub>"))
                {
                    t.Inlines.Add(new Run(GetStringInTag("</sub>", ref i, ref plaintext, t, ref str))
                    {
                        BaselineAlignment = BaselineAlignment.Subscript, FontSize = sub
                    });
                }
                else if (str.StartsWith("<sup>"))
                {
                    t.Inlines.Add(new TextBlock()
                    {
                        Text = GetStringInTag("</sup>", ref i, ref plaintext, t, ref str), BaselineOffset = size * 1.3, FontSize = sub * 0.8
                    });
                }
                else if (str.StartsWith("<dsup>"))
                {
                    t.Inlines.Add(new Run(GetStringInTag("</dsup>", ref i, ref plaintext, t, ref str))
                    {
                        BaselineAlignment = BaselineAlignment.Superscript, FontSize = sub * 0.8
                    });
                }
                else if (str.StartsWith("<hsup>"))
                {
                    t.Inlines.Add(new TextBlock()
                    {
                        Text = GetStringInTag("</hsup>", ref i, ref plaintext, t, ref str), BaselineOffset = size * 1.8, FontSize = sub * 0.8, Margin = new Thickness(-10, 0, 0, 0)
                    });
                }
                else if (str.StartsWith("<formula>"))
                {
                    string    texf = GetStringInTag("</formula>", ref i, ref plaintext, t, ref str);
                    TextBlock tb   = FormulaParser.Parse(texf, (int)f, dict, iswhite);
                    tb.Foreground = Brushes.Black;
                    if (scroll)
                    {
                        ScrollViewer s = new ScrollViewer()
                        {
                            HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, VerticalScrollBarVisibility = ScrollBarVisibility.Disabled
                        };
                        s.MouseWheel += (ss, ee) => ee.Handled = true;
                        s.Content     = tb;
                        t.Inlines.Add(s);
                    }
                    else
                    {
                        t.Inlines.Add(tb);
                    }
                }
                else if (str.StartsWith("<Bold>"))
                {
                    t.Inlines.Add(new Run(GetStringInTag("</Bold>", ref i, ref plaintext, t, ref str))
                    {
                        FontWeight = FontWeights.Bold, Foreground = Brushes.Blue
                    });
                }
                else if (str.StartsWith("<sBold>"))
                {
                    t.Inlines.Add(new Run(GetStringInTag("</sBold>", ref i, ref plaintext, t, ref str))
                    {
                        FontWeight = FontWeights.Bold, Foreground = Brushes.Blue, BaselineAlignment = BaselineAlignment.Subscript, FontSize = sub
                    });
                }
                else if (str.StartsWith("<redBold>"))
                {
                    t.Inlines.Add(new Run(GetStringInTag("</redBold>", ref i, ref plaintext, t, ref str))
                    {
                        FontWeight = FontWeights.Bold, Foreground = Brushes.Red
                    });
                }
                else if (str.StartsWith("<i>"))
                {
                    t.Inlines.Add(new Run(GetStringInTag("</i>", ref i, ref plaintext, t, ref str))
                    {
                        FontStyle = FontStyles.Italic
                    });
                }
                else if (str.StartsWith("<si>"))
                {
                    t.Inlines.Add(new Run(GetStringInTag("</si>", ref i, ref plaintext, t, ref str))
                    {
                        FontStyle = FontStyles.Italic, BaselineAlignment = BaselineAlignment.Subscript, FontSize = sub
                    });
                }
                else
                {
                    plaintext += str[0];
                    ++i;
                }
                str = str.Substring(i);
            }
            if (plaintext != "")
            {
                t.Inlines.Add(plaintext);
            }
            return(t);
        }
        public TestQuestion(Question question_data, Action <bool> ShowResult)
        {
            InitializeComponent();

            if (question_data.Name != null)
            {
                TextBlock question = new TextBlock()
                {
                    FontSize      = 40,
                    FontWeight    = FontWeights.Bold,
                    TextWrapping  = TextWrapping.Wrap,
                    TextAlignment = TextAlignment.Center
                };
                question.Text = question_data.Name;

                content.Children.Add(question);
            }
            if (question_data.Image != null)
            {
                Image image = new Image()
                {
                    Source   = new BitmapImage(new Uri(question_data.Image)),
                    MaxWidth = question_data.Width,
                    Margin   = new Thickness(0, 15, 0, 0)
                };
                content.Children.Add(image);
            }

            answers         = question_data.Answers;
            this.ShowResult = ShowResult;
            foreach (var a in answers)
            {
                if (question_data.ManyAnswers)
                {
                    if (a.Text != null)
                    {
                        if (a.Text.Contains("<formula>"))
                        {
                            TextBlock   formula     = FormulaParser.Parse(Helpers.GetStringInTag("</formula>", a.Text), 30, null, false);
                            MyTextBlock myTextBlock = new MyTextBlock(formula)
                            {
                                IsRight = a.IsRight, IsFormula = true
                            };
                            stackAnswers.Children.Add(new CheckBox()
                            {
                                Content = myTextBlock
                            });
                        }
                        else
                        {
                            TextBlock   text        = Helpers.GetFormattedText(a.Text, 30);
                            MyTextBlock myTextBlock = new MyTextBlock(text)
                            {
                                IsRight = a.IsRight
                            };
                            stackAnswers.Children.Add(new CheckBox()
                            {
                                Content = myTextBlock
                            });
                        }
                    }
                    else if (a.Image != null)
                    {
                        stackAnswers.Children.Add(new CheckBox()
                        {
                            Content = new Image()
                            {
                                MaxWidth = a.Width, Source = new BitmapImage(new Uri(a.Image))
                            }
                        });
                    }
                }
                else
                {
                    if (a.Text != null)
                    {
                        if (a.Text.Contains("<formula>"))
                        {
                            TextBlock   formula     = FormulaParser.Parse(Helpers.GetStringInTag("</formula>", a.Text), 30, null, false);
                            MyTextBlock myTextBlock = new MyTextBlock(formula)
                            {
                                IsRight = a.IsRight, IsFormula = true
                            };
                            stackAnswers.Children.Add(new RadioButton()
                            {
                                GroupName = "variants", Content = myTextBlock
                            });
                        }
                        else
                        {
                            TextBlock   text        = Helpers.GetFormattedText(a.Text, 30);
                            MyTextBlock myTextBlock = new MyTextBlock(text)
                            {
                                IsRight = a.IsRight
                            };
                            stackAnswers.Children.Add(new RadioButton()
                            {
                                GroupName = "variants", Content = myTextBlock
                            });
                        }
                    }

                    else if (a.Image != null)
                    {
                        stackAnswers.Children.Add(new RadioButton()
                        {
                            Content = new Image()
                            {
                                MaxWidth = a.Width, Source = new BitmapImage(new Uri(a.Image)), Margin = new Thickness(3)
                            }
                        });
                    }
                }
            }
        }