使用 PixelsPerDip 覆盖?WPF .NET 4.8 从 .NET 4.6 升级

gch*_*chq 1 .net vb.net wpf

我们刚刚将 .NET 4.6 项目升级到 .NET 4.8,并且此功能

Private Function MeasureTextSize(ByVal text As String, ByVal fontFamily As FontFamily, ByVal fontStyle As FontStyle, ByVal fontWeight As FontWeight, ByVal fontStretch As FontStretch, ByVal fontSize As Double) As Size

    Dim ft As New FormattedText(text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, New Typeface(fontFamily, fontStyle, fontWeight, fontStretch), fontSize, Brushes.Black)
    Return New Size(ft.Width, ft.Height)
End Function
Run Code Online (Sandbox Code Playgroud)

显示以下警告

warning BC40000: 'Public Overloads Sub New(textToFormat As String, culture As CultureInfo, flowDirection As FlowDirection, typeface As Typeface, emSize As Double, foreground As Brush)' is obsolete: 'Use the PixelsPerDip override'.
Run Code Online (Sandbox Code Playgroud)

记住这是一个模块,如何纠正?

谢谢

mm8*_*mm8 5

基本上你有三个选择:

  • 创建视觉对象并使用该GetDpi方法获取参数值pixelsPerDip

    VisualTreeHelper.GetDpi(new Button()).PixelsPerDip
    
    Run Code Online (Sandbox Code Playgroud)
  • 自己定义一个静态值,例如1.25.

  • 忽略警告并使用过时的重载。