Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin.forms 像素到Xamarin。表单字体大小_Xamarin.forms_Pixel - Fatal编程技术网

Xamarin.forms 像素到Xamarin。表单字体大小

Xamarin.forms 像素到Xamarin。表单字体大小,xamarin.forms,pixel,Xamarin.forms,Pixel,我正在使用Xamarin表单,我有一个项目要求字体大小为32像素。我一直在谷歌上搜索,但是我找不到一个关于如何在Xamarin设备独立单元中转换这些像素的答案。我将非常感谢您的帮助 谢谢。您可以使用调用本机方法来设置32像素。基于按钮控件,您可以按如下方式创建Xamaarin表单中的自定义按钮: public class MyButton : Button { } [assembly: ExportRenderer(typeof(Xamarin.Forms.MyButton), typeof(

我正在使用Xamarin表单,我有一个项目要求字体大小为32像素。我一直在谷歌上搜索,但是我找不到一个关于如何在Xamarin设备独立单元中转换这些像素的答案。我将非常感谢您的帮助

谢谢。

您可以使用调用本机方法来设置32像素。基于按钮控件,您可以按如下方式创建Xamaarin表单中的自定义
按钮

public class MyButton : Button
{
}
[assembly: ExportRenderer(typeof(Xamarin.Forms.MyButton), typeof(CustomButtonRenderer))]
namespace AppShellTest.Droid
{
    [Obsolete]
    public class CustomButtonRenderer: ButtonRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                // do whatever you want to the UITextField here!
                Control.SetTextSize(Android.Util.ComplexUnitType.Px, 32);
            }
        }
    }
}
然后,Andoird解决方案创建自定义渲染类,如下所示:

public class MyButton : Button
{
}
[assembly: ExportRenderer(typeof(Xamarin.Forms.MyButton), typeof(CustomButtonRenderer))]
namespace AppShellTest.Droid
{
    [Obsolete]
    public class CustomButtonRenderer: ButtonRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                // do whatever you want to the UITextField here!
                Control.SetTextSize(Android.Util.ComplexUnitType.Px, 32);
            }
        }
    }
}
然后,渲染器代码将为:

public class CustomButtonRenderer: ButtonRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            // do whatever you want to the UITextField here!
            Control.SetTextSize(Android.Util.ComplexUnitType.Px, dip2px(32));
        }
    }

    public int dip2px(float dpValue)
    {
        float scale = MainActivity.instance.Resources.DisplayMetrics.Density;
        return (int)(dpValue * scale + 0.5f);
    }
}
Xamarin在任何地方都使用DIU(设备独立单元)。 看看

我也有类似的问题,通过反复试验发现Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Density返回设备上每DIU的像素数(在iPod Touch、诺基亚8和Firetablet上测试)

所以这应该是正确的:

myLabel.FontSize = 32 / Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Density;
但正如Jason所说,您可能需要检查您的要求,因为设备上的像素越多,字体越小。这就是人们首先使用DIU的原因:-)
也许最好看看客户主页上的字体有多大。也许在那里以英寸、厘米或任何适合你的单位测量,然后进行数学运算,将其转换为DIU(见上面的链接,64 DIU=1厘米)。

我会回过头来,一些设备的像素密度超过500 PPI,这意味着32像素字体是不可理解的,这就是为什么当我尝试在xxxhdpi中将px转换为dp时,字体几乎无法读取。谢谢,当我在>500ppi设备中使用32像素的代码时,字体太小了。我认为他们的网站采用了使用像素作为字体大小的大小,有没有办法将dp与网站像素匹配?我会更新答案,您可以检查转换方法是否适合您。