Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the PixelsPerDip override? WPF .NET 4.8 updrade from .NET 4.6

Tags:

.net

vb.net

wpf

We have just upgraded a .NET 4.6 project to .NET 4.8 and this function

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

Is showing the following warning

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'.

Bearing in mind this is a module, how can this be corrected?

Thank you

like image 832
gchq Avatar asked Dec 15 '25 03:12

gchq


2 Answers

You have three options basically:

  • Create a visual and use the GetDpi method to get a value for the pixelsPerDip parameter:

    VisualTreeHelper.GetDpi(new Button()).PixelsPerDip
    
  • Define a static value yourself, e.g. 1.25.

  • Ignore the warning and use the obsolete overload.

like image 105
mm8 Avatar answered Dec 16 '25 17:12

mm8


The warning message is telling you that the overload of New FormattedText() that you are using is obsolete, but there exists another overload of the same constructor which is not obsolete and you should use.

So, the solution you are looking for is to replace this:

New FormattedText( ... )

with this:

New FormattedText( ..., 1.0 );
like image 22
Mike Nakis Avatar answered Dec 16 '25 18:12

Mike Nakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!