Xamarin 根据屏幕大小自定义命名字体大小

Xamarin 根据屏幕大小自定义命名字体大小,xamarin,xamarin.forms,Xamarin,Xamarin.forms,在xamarin.forms应用程序中,如何根据屏幕大小自定义命名字体大小,以使默认字体不适合我 mainScale.LabelFontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)) * 2.5; 我硬编码了2.5,因为它看起来更好 您能告诉我如何在跨平台应用程序中将这些硬编码更改为动态因素吗?这是一个如何创建尺寸的想法 App.xaml.cs public static double DisplayScreenWidth

在xamarin.forms应用程序中,如何根据屏幕大小自定义命名字体大小,以使默认字体不适合我

mainScale.LabelFontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)) * 2.5;
我硬编码了2.5,因为它看起来更好


您能告诉我如何在跨平台应用程序中将这些硬编码更改为动态因素吗?

这是一个如何创建尺寸的想法

App.xaml.cs

public static double DisplayScreenWidth = 0f;
public static double DisplayScreenHeight = 0f;

public static double Size1 { get; private set; }
public static double Size2 { get; private set; }
public static double Size3 { get; private set; }

if(DisplayScreenHeight > 560)
{
     Size1 = Device.GetNamedSize(NamedSize.Large, typeof(Label)) * 1.6;
}
else
{
     Size1 = Device.GetNamedSize(NamedSize.Large, typeof(Label)) * 1.2;
}
Activity.cs

App.DisplayScreenWidth = (double)Resources.DisplayMetrics.WidthPixels / (double)Resources.DisplayMetrics.Density;
App.DisplayScreenHeight = (double)Resources.DisplayMetrics.HeightPixels / (double)Resources.DisplayMetrics.Density;
App.xaml

xmlns:local="clr-namespace:Mobile" >

<Style x:Key="baseStyle" TargetType="Label">
            <Style.Triggers>
                <Trigger  TargetType="Label"
                       Property="FontAttributes" 
                       Value="None">
                    <Setter Property="FontFamily" Value="OpenSans-Regular.ttf#Regular" />
                </Trigger>
                <Trigger  TargetType="Label"
                       Property="FontAttributes" 
                       Value="Bold">
                    <Setter Property="FontFamily" Value="OpenSans-Bold.ttf#Regular-Bold" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <!-- TAMANHO DE FONTES -->
        <Style x:Key="Size1" TargetType="Label" BasedOn="{StaticResource baseStyle}">
            <Setter Property="FontSize" Value="{Binding Source={x:Static local:App.Size1}}"/>
        </Style>
        <Style x:Key="Size2" TargetType="Label" BasedOn="{StaticResource baseStyle}">
            <Setter Property="FontSize" Value="{Binding Source={x:Static local:App.Size2}}"/> 
        </Style>
xmlns:local=“clr namespace:Mobile”>
Home.xaml

<label Text="Hello from Xamarin Forms" Style="{StaticResource Size1}"/>

我遇到了Allan Ritchie提供的跨平台屏幕分辨率解决方案,nuget软件包是

在共享的.net标准库中,在相应的ContentView中,以下代码起作用:

    protected override void OnSizeAllocated(double width, double height)
    {
        // specific font adjustment code goes here
        if (CrossDevice.Hardware.ScreenHeight > 600)
        {
        ...              
        }
        ...

        base.OnSizeAllocated(width, height); //must be called
    }

谢谢,我得到了参考资料。DisplayMetrics未找到错误。你能告诉我怎样才能使DisplayMetrics就位吗?。这张图片可能会对你有所帮助。我已经添加了解决跨平台问题的答案。非常感谢你的帮助,艾伦。