如何在WinRT应用程序中获取字体的大小(以像素为单位)?

正如标题所述,在.NET 4.5中我们有一个字体类可以给你像素的高度,但在WinRT中怎么样?

我可以使用任何API来获取它使用的像素吗?

由于甚至FormattedText类都不存在于Windowsapp store的.NET API中,我的解决方法是使用TextBlock:

TextBlock dummyTextBlock = new TextBlock(); dummyTextBlock.FontFamily = new FontFamily("Tahoma"); dummyTextBlock.FontSize = 18; dummyTextBlock.FontStyle = FontStyle.Normal; dummyTextBlock.FontWeight = FontWeights.Bold; dummyTextBlock.Text = "X"; dummyTextBlock.Measure(new Size(0,0)); dummyTextBlock.Arrange(new Rect(0,0,0,0)); double width = dummyTextBlock.ActualWidth; double height = dummyTextBlock.ActualHeight; 

这为您提供了文本的高度(和宽度)显示方式。