Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit c5bea02

Browse files
authored
FormsFontIcon added (#9587)
1 parent 08007a8 commit c5bea02

File tree

10 files changed

+187
-3
lines changed

10 files changed

+187
-3
lines changed

Xamarin.Forms.Controls/ControlGalleryPages/ToolbarItems.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ public ToolbarItems()
2323
tb1.IsEnabled = _isEnable;
2424
tb1.AutomationId = "toolbaritem_primary";
2525

26-
var tb2 = new ToolbarItem("tb2", null, () =>
26+
var fis = new FontImageSource()
27+
{
28+
FontFamily = GetFontFamily(),
29+
Glyph = '\uf101'.ToString(),
30+
Color = Color.Red
31+
};
32+
33+
var tb2 = new ToolbarItem("tb2 font", null, () =>
2734
{
2835
label.Text = "tb2";
2936
}, ToolbarItemOrder.Primary);
37+
tb2.IconImageSource = fis;
3038
tb2.AutomationId = "toolbaritem_primary2";
3139
var tb6 = new ToolbarItem("tb6 long long text", null, () =>
3240
{
@@ -72,6 +80,31 @@ public ToolbarItems()
7280
}
7381
};
7482
}
83+
84+
static string GetFontFamily()
85+
{
86+
var fontFamily = "";
87+
switch (Device.RuntimePlatform)
88+
{
89+
case Device.macOS:
90+
case Device.iOS:
91+
fontFamily = "Ionicons";
92+
break;
93+
case Device.UWP:
94+
fontFamily = "Assets/Fonts/ionicons.ttf#ionicons";
95+
break;
96+
case Device.WPF:
97+
case Device.GTK:
98+
fontFamily = "Assets/ionicons.ttf#ionicons";
99+
break;
100+
case Device.Android:
101+
default:
102+
fontFamily = "fonts/ionicons.ttf#";
103+
break;
104+
}
105+
106+
return fontFamily;
107+
}
75108
}
76109
}
77110

Xamarin.Forms.Controls/GalleryPages/FontImageSourceGallery.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,34 @@ public FontImageSourceGallery()
6868
HorizontalOptions = LayoutOptions.FillAndExpand,
6969
Content = grid
7070
};
71+
72+
var tb1 = new ToolbarItem()
73+
{
74+
Text = "tb1",
75+
IconImageSource = new FontImageSource()
76+
{
77+
FontFamily = fontFamily, Glyph = '\uf101'.ToString()
78+
}
79+
};
80+
var tb2 = new ToolbarItem
81+
{
82+
Text = "tb2 red",
83+
IconImageSource = new FontImageSource()
84+
{
85+
FontFamily = fontFamily, Glyph = '\uf101'.ToString(), Color = Color.Red
86+
},
87+
};
88+
var tb3 = new ToolbarItem
89+
{
90+
Text = "tb3 yellow",
91+
IconImageSource = new FontImageSource()
92+
{
93+
FontFamily = fontFamily, Glyph = '\uf2c7'.ToString(), Color = Color.Yellow
94+
},
95+
};
96+
ToolbarItems.Add(tb1);
97+
ToolbarItems.Add(tb2);
98+
ToolbarItems.Add(tb3);
7199
}
72100

73101
static readonly char[] Ionicons = new char[] {

Xamarin.Forms.Platform.WPF/Assets/Converters.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
<converters:SymbolToValueConverter x:Key="SymbolToValue" />
66
<BooleanToVisibilityConverter x:Key="BoolToVis" />
77
<converters:ToUpperConverter x:Key="ToUpperConverter" />
8-
8+
<converters:FontFamilyConveter x:Key="FontFamilyConveter" />
9+
<converters:FontIconColorConverter x:Key="FontIconColorConverter" />
910
</ResourceDictionary>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace Xamarin.Forms.Platform.WPF.Controls
10+
{
11+
public class FormsFontIcon : FormsElementIcon
12+
{
13+
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("UriSource", typeof(FontImageSource), typeof(FormsFontIcon), new PropertyMetadata(OnSourceChanged));
14+
15+
public FontImageSource Source
16+
{
17+
get { return (FontImageSource)GetValue(SourceProperty); }
18+
set { SetValue(SourceProperty, value); }
19+
}
20+
21+
public FormsFontIcon()
22+
{
23+
this.DefaultStyleKey = typeof(FormsFontIcon);
24+
}
25+
26+
private static void OnSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
27+
{
28+
((FormsFontIcon)o).OnSourceChanged(e.OldValue, e.NewValue);
29+
}
30+
31+
private void OnSourceChanged(object oldValue, object newValue)
32+
{
33+
if (newValue is FontImageSource src)
34+
{
35+
Source = src;
36+
}
37+
}
38+
}
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Media;
4+
5+
namespace Xamarin.Forms.Platform.WPF.Converters
6+
{
7+
public class FontFamilyConveter : System.Windows.Data.IValueConverter
8+
{
9+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
10+
{
11+
if (value is string val && !string.IsNullOrEmpty(val))
12+
{
13+
return new FontFamily(new Uri("pack://application:,,,"), val);
14+
}
15+
16+
return null;
17+
}
18+
19+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
20+
{
21+
throw new NotImplementedException();
22+
}
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Globalization;
3+
4+
namespace Xamarin.Forms.Platform.WPF.Converters
5+
{
6+
public sealed class FontIconColorConverter : System.Windows.Data.IValueConverter
7+
{
8+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
9+
{
10+
if (value is Color c && !c.IsDefault)
11+
{
12+
return c.ToBrush();
13+
}
14+
15+
return Color.White.ToBrush();
16+
}
17+
18+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
19+
{
20+
throw new NotImplementedException();
21+
}
22+
}
23+
}

Xamarin.Forms.Platform.WPF/Converters/IconConveter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Globalization;
33
using System.IO;
4+
using System.Windows;
45
using System.Windows.Media;
56
using Xamarin.Forms.Platform.WPF.Controls;
67
using Xamarin.Forms.Platform.WPF.Enums;
@@ -20,6 +21,10 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
2021
else if (Path.GetExtension(imageSource.File) != null)
2122
return new FormsBitmapIcon() { UriSource = new Uri(imageSource.File, UriKind.RelativeOrAbsolute) };
2223
}
24+
else if (value is FontImageSource fontsource)
25+
{
26+
return new FormsFontIcon() { Source = fontsource };
27+
}
2328

2429
return null;
2530
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:controls="clr-namespace:Xamarin.Forms.Platform.WPF.Controls">
4+
<ResourceDictionary.MergedDictionaries>
5+
<ResourceDictionary Source="/Xamarin.Forms.Platform.WPF;component/Assets/Converters.xaml" />
6+
</ResourceDictionary.MergedDictionaries>
7+
8+
<Style TargetType="controls:FormsFontIcon">
9+
<Setter Property="HorizontalAlignment" Value="Stretch"/>
10+
<Setter Property="VerticalAlignment" Value="Stretch"/>
11+
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
12+
<Setter Property="FontSize" Value="22"/>
13+
<Setter Property="Template">
14+
<Setter.Value>
15+
<ControlTemplate TargetType="controls:FormsFontIcon">
16+
<TextBlock Text="{Binding Source.Glyph, RelativeSource={RelativeSource Mode=TemplatedParent}}"
17+
FontFamily="{Binding Source.FontFamily, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource FontFamilyConveter}}"
18+
Foreground="{Binding Source.Color, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource FontIconColorConverter}}">
19+
</TextBlock>
20+
</ControlTemplate>
21+
</Setter.Value>
22+
</Setter>
23+
</Style>
24+
</ResourceDictionary>

Xamarin.Forms.Platform.WPF/Themes/Generic.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<!-- WindowButtonCommands -->
2323
<ResourceDictionary Source="/Xamarin.Forms.Platform.WPF;component/Themes/FormsWindowButtonCommands.xaml" />
2424

25-
25+
<ResourceDictionary Source="/Xamarin.Forms.Platform.WPF;component/Themes/FormsFontIcon.xaml" />
2626
<ResourceDictionary Source="/Xamarin.Forms.Platform.WPF;component/Themes/FormsSymbolIcon.xaml" />
2727
<ResourceDictionary Source="/Xamarin.Forms.Platform.WPF;component/Themes/FormsBitmapIcon.xaml" />
2828
<ResourceDictionary Source="/Xamarin.Forms.Platform.WPF;component/Themes/FormsPathIcon.xaml" />

Xamarin.Forms.Platform.WPF/Xamarin.Forms.Platform.WPF.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Compile Include="CellControl.cs" />
6363
<Compile Include="Controls\FormsAppBar.cs" />
6464
<Compile Include="Controls\FormsAppBarButton.cs" />
65+
<Compile Include="Controls\FormsFontIcon.cs" />
6566
<Compile Include="Controls\FormsBitmapIcon.cs" />
6667
<Compile Include="Controls\FormsButton.cs" />
6768
<Compile Include="Controls\FormsCarouselPage.cs" />
@@ -86,6 +87,8 @@
8687
<Compile Include="Converters\CaseConverter.cs" />
8788
<Compile Include="Converters\CollapseWhenEmptyConverter.cs" />
8889
<Compile Include="Converters\ColorConverter.cs" />
90+
<Compile Include="Converters\FontFamilyConveter.cs" />
91+
<Compile Include="Converters\FontIconColorConverter.cs" />
8992
<Compile Include="Converters\HeightConverter.cs" />
9093
<Compile Include="Converters\IconConveter.cs" />
9194
<Compile Include="Converters\ImageConverter.cs" />
@@ -230,6 +233,10 @@
230233
<Generator>MSBuild:Compile</Generator>
231234
<SubType>Designer</SubType>
232235
</Page>
236+
<Page Include="Themes\FormsFontIcon.xaml">
237+
<Generator>MSBuild:Compile</Generator>
238+
<SubType>Designer</SubType>
239+
</Page>
233240
<Page Include="Controls\FormsProgressRing.xaml">
234241
<Generator>MSBuild:Compile</Generator>
235242
</Page>

0 commit comments

Comments
 (0)