1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Xamarin.Forms.Internals;
using WStyle = Windows.UI.Xaml.Style;
#if WINDOWS_UWP
namespace Xamarin.Forms.Platform.UWP
#else
namespace Xamarin.Forms.Platform.WinRT
#endif
{
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),
#if WINDOWS_UWP
[Device.Styles.ListItemTextStyleKey] = GetStyle("BaseTextBlockStyle", prototype),
#else
[Device.Styles.ListItemTextStyleKey] = GetStyle("TitleTextBlockStyle", prototype),
#endif
};
}
Style GetStyle(object nativeKey, TextBlock prototype)
{
var style = (WStyle)Windows.UI.Xaml.Application.Current.Resources[nativeKey];
prototype.Style = style;
var formsStyle = new Style(typeof(Label));
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));
return formsStyle;
}
static FontAttributes ToAttributes(FontWeight fontWeight)
{
if (fontWeight.Weight == FontWeights.Bold.Weight || fontWeight.Weight == FontWeights.SemiBold.Weight
|| fontWeight.Weight == FontWeights.ExtraBold.Weight)
{
return FontAttributes.Bold;
}
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;
}
}
}
}
|