FormattedText.FormttedText 已过时。使用 PixelsPerDip 覆盖

FormattedText.FormttedText is obsolete. Use the PixelsPerDip override

我正在尝试将标签填充到水平滑块,并且通过将 Text 传递给 FormattedText 构造函数,我使用从 TickBar 派生的 class 成功地做到了这一点。但是现在,当我使用相同的代码并将其粘贴到使用 .NET Framework 4.6.2 版的 Visual Studio 中时,它会显示:

FormattedText.FormttedText 已过时。使用 PixelsPerDip 覆盖。

我参考了

但是在当前的情况下我该如何使用它。请帮忙。

FormattedText formattedText = new FormattedText(text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 16, Brushes.Black);
dc.DrawText(formattedText, new Point((tickFrequencySize * i), 30)); //dc is Drawing Context.

这是完整的 class:

public class CustomTickBar : TickBar
    {
        public static string FontTextList { get; set; }
        protected override void OnRender(DrawingContext dc)
        {
            //string str = "Small, Medium, Large, Extra\n Large";
            if (!string.IsNullOrEmpty(FontTextList))
            {
                string[] ar = FontTextList.Split(',');

                Size size = new Size(base.ActualWidth, base.ActualHeight);
                int tickCount = (int)((this.Maximum - this.Minimum) / this.TickFrequency) + 1;
                //int tickCount = 4; 
                if ((this.Maximum - this.Minimum) % this.TickFrequency == 0)
                    tickCount -= 1;
                Double tickFrequencySize;
                // Calculate tick's setting
                tickFrequencySize = (size.Width * this.TickFrequency / (this.Maximum - this.Minimum));
                string text = "";
                FormattedText formattedText = null;
                double num = this.Maximum - this.Minimum;
                int i = 0;
                // Draw each tick text
                for (i = 0; i <= tickCount; i++)
                {

                    //text = Convert.ToString(Convert.ToInt32(this.Minimum + this.TickFrequency * i), 10);
                    text = ar[i];
                    formattedText = new FormattedText(text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 16, Brushes.Black);
                    dc.DrawText(formattedText, new Point((tickFrequencySize * i), 30));


                }
            }
        }

试试这个:

FormattedText formattedText = new FormattedText(text, CultureInfo.GetCultureInfo("en-us"),
    FlowDirection.LeftToRight, new Typeface("Verdana"), 16, Brushes.Black,
    VisualTreeHelper.GetDpi(this).PixelsPerDip);