
1、UI使用hc库
xaml界面设计如下:
<UserControl
x:Class="YOI.Core.Views.DialogPages.ToolView.BrowseTTFView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:local="clr-namespace:YOI.Core.Views.DialogPages.ToolView"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
Width="600"
Height="500"
prism:ViewModelLocator.AutoWireViewModel="True"
Background="{DynamicResource SecundaryBackgroundColor}"
FontWeight="Bold"
mc:Ignorable="d">
<UserControl.Resources>
<FontFamily x:Key="ttfIcon">
pack://application:,,,/Assets/Themes/Icons/#Iconfont
</FontFamily>
</UserControl.Resources>
<prism:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="Width" Value="600" />
<Setter Property="Height" Value="500" />
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="WindowState" Value="Normal" />
<Setter Property="WindowChrome.CornerRadius" Value="5" />
<Setter Property="ShowInTaskbar" Value="False" />
<Setter Property="Topmost" Value="True" />
<Setter Property="WindowChrome.GlassFrameThickness" Value="-1" />
<Setter Property="prism:Dialog.WindowStartupLocation" Value="CenterScreen" />
</Style>
</prism:Dialog.WindowStyle>
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<DockPanel Grid.Row="0" VerticalAlignment="Center">
<TextBlock
Width="520"
Margin="18,0,0,0"
HorizontalAlignment="Left"
FontSize="16"
FontWeight="Bold"
Foreground="{DynamicResource TextSecundaryColor}"
MouseMove="MsgView_MouseMove"
Text="{Binding Title}" />
<Button
HorizontalAlignment="Right"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
Command="{Binding CancelCommand}"
Content=""
Cursor="Hand"
Style="{DynamicResource MouseOverButton}"
Tag="IsBtnClose" />
</DockPanel>
<DataGrid
Grid.Row="1"
Margin="10,0,10,0"
AutoGenerateColumns="False"
ItemsSource="{Binding IconModels, Mode=TwoWay}"
RowHeaderWidth="80"
RowHeight="80">
<DataGrid.Columns>
<DataGridTextColumn
Width="200"
Binding="{Binding UnCode, Mode=TwoWay}"
FontFamily="{DynamicResource ttfIcon}"
FontSize="14"
Foreground="Black"
Header="UnCode"
IsReadOnly="False" />
<DataGridTemplateColumn Width="200" Header="图标">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Width="200"
Height="Auto"
FontFamily="{DynamicResource ttfIcon}"
FontSize="32"
Foreground="Black"
Text="{Binding Icon, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<hc:Pagination
Grid.Row="2"
Margin="10,10,10,10"
IsJumpEnabled="False"
MaxPageCount="10"
PageIndex="{Binding PageIndex}">
<hc:Interaction.Triggers>
<hc:EventTrigger EventName="PageUpdated">
<hc:EventToCommand Command="{Binding PageUpdatedCmd}" PassEventArgsToCommand="True" />
</hc:EventTrigger>
</hc:Interaction.Triggers>
</hc:Pagination>
</Grid>
</UserControl>
2、后台关键代码
public void Init()
{
iconModels = new ObservableCollection<IconModel>();
var families = Fonts.GetFontFamilies(@"D:\Code\YOIWindowApps\YOI.Core\Assets\Themes\Icons\iconfont.ttf");
foreach (System.Windows.Media.FontFamily family in families)
{
var typefaces = family.GetTypefaces();
foreach (Typeface typeface in typefaces)
{
GlyphTypeface glyph;
typeface.TryGetGlyphTypeface(out glyph);
IDictionary<int, ushort> characterMap = glyph.CharacterToGlyphMap;
foreach (KeyValuePair<int, ushort> item in characterMap)
{
var tempStr = item.Key.ToString("x8") + ";" ;
var tempUnCode = tempStr.Replace("0000", @"&#x");
var newTempStr = UnicodeToStr(tempUnCode);
UnCodes = newTempStr;
iconModels.Add(new IconModel
{
UnCode = tempUnCode,
Icon = newTempStr
});
}
}
}
}
/// <summary>
/// Unicode 转 字符串
/// </summary>
/// <param name="unicodeStr"></param>
/// <returns></returns>
public string UnicodeToStr(string unicodeStr)
{
string outStr = "";
if (!string.IsNullOrEmpty(unicodeStr))
{
string[] strlist = unicodeStr.Replace("&#", "").Replace(";", "").Split('x');
try
{
for (int i = 1; i < strlist.Length; i++)
{
outStr += (char)int.Parse(strlist[i], System.Globalization.NumberStyles.HexNumber);
}
}
catch (FormatException ex)
{
outStr = ex.Message;
}
}
return outStr;
}