在 .NET Framework 4.6.2 中,FormattedText() 已过时,我该如何修复它 [英] In .NET Framework 4.6.2 the FormattedText() is Obsoleted, how can I fix it
问题描述
当我尝试使用 .net framework 4.6.2 构建 WPF 项目时,出现错误,因为 FormattedText() 已过时,如下所示:[过时(使用 PixelsPerDip 覆盖",false)]public FormattedText(string textToFormat, CultureInfoculture, FlowDirection flowDirection, Typeface typeface, double emSize, Brush foreground);
When I try to build the WPF project with .net framework 4.6.2, I got an error, Because the FormattedText() is Obsoleted as below: [Obsolete("Use the PixelsPerDip override", false)] public FormattedText(string textToFormat, CultureInfo culture, FlowDirection flowDirection, Typeface typeface, double emSize, Brush foreground);
新的覆盖方法是public FormattedText(string textToFormat, CultureInfo culture, FlowDirection flowDirection, Typeface typeface, double emSize, Brush foreground, double pixelPerDip);
问:如何确定pixelsPerDip?
Q: How can I determine the pixelsPerDip ?
问:我如何在没有 pixelPerDip 的情况下使用旧的构造函数?,因为 pixelPerDip 对我的项目没用.
Q: How can I use old constructor without pixelsPerDip?, because the pixelsPerDip is useless for my project.
推荐答案
你需要计算你的显示器的 DPI,见:如何在 WPF 中获取 DPI?
You need to calculate the DPI of your monitor, see: How can I get the DPI in WPF?
此外,.Net 4.6.2 带来了处理 DPI 感知的新 API,因此上述方法可能会被弃用(例如 VisualTreeHelper.GetDpi()).请参阅 https://blogs.msdn.microsoft.com/dotnet/2016/08/02/annoucing-net-framework-4-6-2/这是一些示例代码和用户指南:https://github.com/Microsoft/WPF-Samples/tree/master/PerMonitorDPI
In Addition, with .Net 4.6.2 come new APIs to handle the DPI awareness, so the above methods might be deprecated (e.g. VisualTreeHelper.GetDpi()). See https://blogs.msdn.microsoft.com/dotnet/2016/08/02/announcing-net-framework-4-6-2/ Here is some example code and a Userguide: https://github.com/Microsoft/WPF-Samples/tree/master/PerMonitorDPI
恕我直言,已添加此参数,以便您的程序可以在具有不同 DPI 的显示器之间拖动,并且仍然可以正确缩放.
IMHO this pararameter has been added so that your program can be dragged between monitors with different DPIs and still is scaled correctly.
来自 FromattedText 声明:pixelsPerDip:
From FromattedText declaration: pixelsPerDip:
Pixels Per Density Independent Pixel 值,即相当于比例因子.例如,如果屏幕的 DPI 为120(或 1.25,因为 120/96 = 1.25),每个密度 1.25 像素绘制独立像素.DIP 是 WPF 使用的度量单位独立于设备分辨率和 DPI.
The Pixels Per Density Independent Pixel value, which is the equivalent of the scale factor. For example, if the DPI of a screen is 120 (or 1.25 because 120/96 = 1.25) , 1.25 pixel per density independent pixel is drawn. DIP is the unit of measurement used by WPF to be independent of device resolution and DPIs.
如果您只有 1 个监视器,因此不需要任何 DPI 更改的事件处理,请在您的 Window(或您的构造函数)的 OnLoaded() 事件中使用以下示例:
If you just have 1 monitor and therefore don't need any DPI changed event handling, use the following for example in the OnLoaded() event of your Window (or in your constructor):
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
这篇关于在 .NET Framework 4.6.2 中,FormattedText() 已过时,我该如何修复它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!