FormattedText.FormttedText is obsolete. Use the PixelsPerDip override

▼魔方 西西 提交于 2020-01-03 19:54:09

问题


I am trying to poplate labels to a horizontal slider and I was successfully able to do it using a class that derives from TickBar by passing the Text to FormattedText constructor. But now when I take the same code and paste in Visual Studio that uses .net framework version 4.6.2 it says,

FormattedText.FormttedText is obsolete. Use the PixelsPerDip override.

I referred to In .NET Framework 4.6.2 the FormattedText() is Obsoleted, how can i fixed it

But how can I use that in this current case. Please help.

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.

Here is the complete 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));


                }
            }
        }

回答1:


Try this:

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


来源:https://stackoverflow.com/questions/45765980/formattedtext-formttedtext-is-obsolete-use-the-pixelsperdip-override

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!