问题描述
例如,我如何将 view.WidthRequest
值转换为平台像素?我正在寻找像 Device.ConvertToPixels(10)
这样的方法.
How can I convert, for example, view.WidthRequest
value to platform pixels? I'm looking for method like Device.ConvertToPixels(10)
.
我想用它来绘制 SkiaSharp.例如,我想在(xamarin.forms 单位)中绘制笔划 = 10 的圆,该圆将在绘制时转换为像素.
I want to use it for SkiaSharp drawing. For example I want to draw circle with stroke = 10 in (xamarin.forms units) which will be converted to pixels on draw.
推荐答案
将 MainDisplayInfo.Dencity 乘以 (xamarin.forms 单位) 即可获得像素.
Multiply MainDisplayInfo.Dencity by (xamarin.forms units) and you get that pixels.
我做了一个方法.
double XamDIUConvertToPixels(double XamDIU)
{
var desplayinfo = DeviceDisplay.MainDisplayInfo;
var pixcels = desplayinfo.Density * XamDIU;
return pixcels;
}
DeviceDisplay 必须在 UI 线程上完成,否则 iOS 会抛出异常.
DeviceDisplay must be done on the UI thread or else an exception will be thrown in iOS.
请阅读:https://docs.microsoft.com/en-us/xamarin/essentials/device-display?tabs=ios#platform-differences
这篇关于将 Xamarin.Forms 度量单位转换为像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!