所以我正在为平板电脑开发一个 Xamarin.Forms
应用程序,现在我需要它也可以在手机上运行 - 太好了,Xamarin.Forms 应该处理所有这些(或者我是这么认为的)。
我在手机上运行了该应用程序,但它没有缩小字体(或任何与此相关的尺寸,但我将从字体开始)
我创建了一个像这样的扩展方法:
[ContentProperty("FontSize")]
public class FontExtension : IMarkupExtension
{
public string FontSize { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
double fontSize;
if (FontSize == null)
throw new Exception("No Font Size provided");
if (!double.TryParse(FontSize, out fontSize))
throw new Exception("Font Size Provided is not a double");
return App.Scaling * fontSize;
}
}
我在我的应用程序中使用它,例如 FontSize={ns:Font 18}
,这将根据 App.Scaling
属性缩小我的应用程序的规模。如果是 Phone
,则此属性只需设置为 0.8
。
我还发现两个链接在 Xamarin.Android
中提供了一种方法,但我正在做一个 Xamarin.Forms
应用程序,因此不适用。 (link1,link2)
是否可以根据设备分辨率而不是Idiom缩小字体?
最佳答案
您可以使用两个附加选项。
OnIdiom
相关值。
我知道您希望完全控制尺寸,但根据我的经验,它在开发过程中不太方便。特别是,每次都定义所有文本大小。最常见的方法是在资源字典中使用Style
。样式定义可能会占用相当多的空间,但是将它们应用到控件会更快。
所以,你可以这样做:
<ResourceDictionary>
<Application.Resources>
<OnIdiom x:TypeArguments="x:Double" x:Key="MySize" Phone="15" Tablet="24"/>
<Style TargetType="Label" x:Key="MyCustomStyle">
<Setter Property="FontSize" Value="{StaticResource MySize}"></Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
和
<Label Style="{StaticResource MyCustomStyle}" Text="My Text"/>
使用
DynamicResource
,因为它是 described here .<Style x:Key="myDerivedCustomStyle" TargetType="Label" BaseResourceKey="myCustomStyle"> ... </Style>
并在 C# 代码中计算您需要的所有值:
var myCustomStyle = new Style (typeof(Label)) {
// calculating and defining required FontSize here
};
Resources = new ResourceDictionary();
Resources.Add ("myCustomStyle", myCustomStyle);
关于c# - Xamarin.Forms 的分辨率相关的 FontSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43122565/