代码之家  ›  专栏  ›  技术社区  ›  George M Ceaser Jr

Xamarin按平台形成FontSize

  •  0
  • George M Ceaser Jr  · 技术社区  · 6 年前

            <OnPlatform x:Key="CustLarge" x:TypeArguments="x:Double">
                <On Platform="iOS|Android">Large</On>
                <On Platform="UWP">24.1</On>
            </OnPlatform>
    

    理论上,在我的XAML中,我只需将所有大字体编码为“custligh”,如下所示:

    FontSize = "{StaticResource CustLarge}" />
    

    有什么想法我可以做到这一点,而不是在平台上做的每一个字体大小在我的应用程序?任何帮助都将不胜感激。

    更新:下面是我所说的屏幕截图。iOS模拟器适用于4.7英寸宽的iPhone 6。Windows10模拟器是一款10.0.15254.9 5英寸的手机。

    iOS emulator

    Windows Mobile Emulator

    你可以看到地图上所有的文字都比它应该的大得多。(我正在与独立按钮右侧的段控件中的文本进行相对比较。)在这两种情况下,fontsize都设置为MICRO。

    回到我的问题-有人知道怎么做吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Elias Nawfal    6 年前

    通过创建一个自定义的ResourceDictionary,并在构造函数中添加FontSizes,我找到了一个解决方法。

    public class SResourceDictionnary : ResourceDictionary
    {
        public SResourceDictionnary()
        {
            Add("Micro", new OnPlatform<double>
            {
                Default = Device.GetNamedSize(NamedSize.Micro, typeof(Label)),
                Platforms = { new On
                    {
                        Value = 12d,
                        Platform = new List<string>{"UWP"}
                    }
                }
            });
            Add("Small", new OnPlatform<double>
            {
                Default = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
                Platforms = { new On
                    {
                        Value = 14d,
                        Platform = new List<string>{"UWP"}
                    }
                }
            });
            Add("Medium", new OnPlatform<double>
            {
                Default = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
                Platforms = { new On
                    {
                        Value = 18d,
                        Platform = new List<string>{"UWP"}
                    }
                }
            });
            Add("Large", new OnPlatform<double>
            {
                Default = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                Platforms = { new On
                    {
                        Value = 20d,
                        Platform = new List<string>{"UWP"}
                    }
                }
            });
        }
    

    然后我将字典合并到App.xaml中,如下所示

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <helpers:SResourceDictionnary></helpers:SResourceDictionnary>
            </ResourceDictionary.MergedDictionaries>
            <Style TargetType="Label">
                <Setter Property="FontSize" Value="{StaticResource Small}" ></Setter>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
    

    唯一的缺点是在Xaml中失去了智能感知

    推荐文章