wpf FormattedText.FormttedText 已过时。使用 PixelsPerDip 覆盖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45765980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FormattedText.FormttedText is obsolete. Use the PixelsPerDip override
提问by nikhil
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,
我正在尝试将标签填充到水平滑块,并且通过将 Text 传递给 FormattedText 构造函数,我成功地使用派生自 TickBar 的类来完成此操作。但是现在,当我使用相同的代码并粘贴到使用 .net 框架版本 4.6.2 的 Visual Studio 中时,它说,
FormattedText.FormttedText is obsolete. Use the PixelsPerDip override.
FormattedText.FormttedText 已过时。使用 PixelsPerDip 覆盖。
I referred to In .NET Framework 4.6.2 the FormattedText() is Obsoleted, how can i fixed it
我提到在 .NET Framework 4.6.2 中,FormattedText() 已过时,我该如何修复它
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));
}
}
}
回答by Olexiy Sadovnikov
Try this:
尝试这个:
FormattedText formattedText = new FormattedText(text, CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight, new Typeface("Verdana"), 16, Brushes.Black,
VisualTreeHelper.GetDpi(this).PixelsPerDip);

