WPF 浏览ttf图标

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="&#xed23;"
                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;
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值