maui-linux/Xamarin.Forms.Platform.UAP/WindowsResourcesProvider.cs

68 строки
2.1 KiB
C#
Исходник Обычный вид История

using Windows.UI.Text;
2016-03-22 23:02:25 +03:00
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Xamarin.Forms.Internals;
using WStyle = Windows.UI.Xaml.Style;
2016-03-22 23:02:25 +03:00
namespace Xamarin.Forms.Platform.UWP
{
internal sealed class WindowsResourcesProvider : ISystemResourcesProvider
{
public IResourceDictionary GetSystemResources()
{
var prototype = new TextBlock();
return new ResourceDictionary
{
[Device.Styles.TitleStyleKey] = GetStyle("HeaderTextBlockStyle", prototype),
[Device.Styles.SubtitleStyleKey] = GetStyle("SubheaderTextBlockStyle", prototype),
[Device.Styles.BodyStyleKey] = GetStyle("BodyTextBlockStyle", prototype),
[Device.Styles.CaptionStyleKey] = GetStyle("CaptionTextBlockStyle", prototype),
[Device.Styles.ListItemDetailTextStyleKey] = GetStyle("BodyTextBlockStyle", prototype),
2016-03-22 23:02:25 +03:00
[Device.Styles.ListItemTextStyleKey] = GetStyle("BaseTextBlockStyle", prototype),
};
2016-03-22 23:02:25 +03:00
}
Style GetStyle(object nativeKey, TextBlock prototype)
2016-03-22 23:02:25 +03:00
{
var style = (WStyle)Windows.UI.Xaml.Application.Current.Resources[nativeKey];
2016-03-22 23:02:25 +03:00
prototype.Style = style;
2016-03-22 23:02:25 +03:00
var formsStyle = new Style(typeof(Label));
2016-03-22 23:02:25 +03:00
formsStyle.Setters.Add(Label.FontSizeProperty, prototype.FontSize);
formsStyle.Setters.Add(Label.FontFamilyProperty, prototype.FontFamily.Source);
formsStyle.Setters.Add(Label.FontAttributesProperty, ToAttributes(prototype.FontWeight));
formsStyle.Setters.Add(Label.LineBreakModeProperty, ToLineBreakMode(prototype.TextWrapping));
2016-03-22 23:02:25 +03:00
return formsStyle;
}
static FontAttributes ToAttributes(FontWeight fontWeight)
2016-03-22 23:02:25 +03:00
{
if (fontWeight.Weight == FontWeights.Bold.Weight || fontWeight.Weight == FontWeights.SemiBold.Weight
|| fontWeight.Weight == FontWeights.ExtraBold.Weight)
{
2016-03-22 23:02:25 +03:00
return FontAttributes.Bold;
}
2016-03-22 23:02:25 +03:00
return FontAttributes.None;
}
static LineBreakMode ToLineBreakMode(TextWrapping value)
{
switch (value)
{
case TextWrapping.Wrap:
return LineBreakMode.CharacterWrap;
case TextWrapping.WrapWholeWords:
return LineBreakMode.WordWrap;
default:
case TextWrapping.NoWrap:
return LineBreakMode.NoWrap;
}
}
}
}