前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >打造自己的PackIcon,开放全域图标定制

打造自己的PackIcon,开放全域图标定制

原创
作者头像
秦建辉
修改2025-02-21 09:05:51
修改2025-02-21 09:05:51
2670
举报
文章被收录于专栏:WPF编程WPF编程

CustomPackIcon.xaml

代码语言:xml
复制
<UserControl
    x:Class="FirstSolver.CustomPackIcon"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Name="UserControlPackIcon">
    <Path
        Name="PathPackIcon"
        Stretch="Uniform" 
        Stroke="{Binding Foreground,ElementName=UserControlPackIcon}"
        StrokeThickness="{Binding StrokeThickness,ElementName=UserControlPackIcon}"
        Data="{Binding Data,ElementName=UserControlPackIcon}"/>
</UserControl>

CustomPackIcon.xaml.cs

代码语言:csharp
复制
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FirstSolver
{
    public partial class CustomPackIcon : UserControl
    {
        private static readonly Lazy<IDictionary<string, string>> s_holder = new Lazy<IDictionary<string, string>>(() => { return JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText($"{AppDomain.CurrentDomain.BaseDirectory}SVG\\{nameof(CustomPackIcon)}.json")) ?? new Dictionary<string, string>(); });

        /// <summary>
        /// SVG图标库源
        /// </summary>
        public static IDictionary<string, string> ItemsSource => s_holder.Value;

        /// <summary>
        /// 构造函数
        /// </summary>
        public CustomPackIcon() => InitializeComponent();

        #region Kind
        /// <summary>
        /// 自定义依赖属性:KindProperty
        /// </summary>
        public static readonly DependencyProperty KindProperty = DependencyProperty.Register(
            nameof(Kind),
            typeof(string),
            typeof(CustomPackIcon),
            new FrameworkPropertyMetadata(
                default(string),
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
                new PropertyChangedCallback((DependencyObject d, DependencyPropertyChangedEventArgs e) =>
                {
                    if (d is CustomPackIcon icon)
                    {
                        if (ItemsSource.TryGetValue(icon.Kind, out string data))
                        {
                            icon.Data = data;
                        }
                    }
                })));

        /// <summary>
        /// 自定义属性:标识 Kind
        /// </summary>
        public string Kind
        {
            get { return (string)GetValue(KindProperty); }
            set { SetValue(KindProperty, value); }
        }
        #endregion

        #region Data
        private static readonly DependencyPropertyKey DataPropertyKey = DependencyProperty.RegisterReadOnly(nameof(Data), typeof(string), typeof(CustomPackIcon), new PropertyMetadata(string.Empty));

        /// <summary>
        /// 自定义依赖属性:DataProperty
        /// </summary>
        public static readonly DependencyProperty DataProperty = DataPropertyKey.DependencyProperty;

        /// <summary>
        /// 自定义属性:路径数据 Data
        /// </summary>
        [TypeConverter(typeof(GeometryConverter))]
        public string Data
        {
            get => (string)GetValue(DataProperty);
            private set => SetValue(DataPropertyKey, value);
        }
        #endregion

        #region StrokeThickness
        /// <summary>
        /// 自定义依赖属性:StrokeThickness
        /// </summary>
        public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register(
            nameof(StrokeThickness),
            typeof(double),
            typeof(CustomPackIcon),
            new PropertyMetadata(1.0));

        /// <summary>
        /// 自定义属性:笔画粗细 StrokeThickness
        /// </summary>
        public double StrokeThickness
        {
            get => (double)GetValue(StrokeThicknessProperty);
            set => SetValue(StrokeThicknessProperty, value);
        }
        #endregion

        #region IsFill
        /// <summary>
        /// 自定义依赖属性:IsFillProperty
        /// </summary>
        public static readonly DependencyProperty IsFillProperty = DependencyProperty.Register(
            nameof(IsFill),
            typeof(bool),
            typeof(CustomPackIcon),
            new PropertyMetadata(false));

        /// <summary>
        /// 自定义属性:是否填充 IsFill
        /// </summary>
        public bool IsFill
        {
            get => (bool)GetValue(IsFillProperty);
            set => SetValue(IsFillProperty, value);
        }
        #endregion

        /// <summary>
        /// 呈现
        /// </summary>
        /// <param name="drawingContext">绘图上下文</param>
        protected override void OnRender(DrawingContext drawingContext)
        {
            PathPackIcon.Fill = IsFill ? PathPackIcon.Stroke : Brushes.Transparent;
            base.OnRender(drawingContext);
        }
    }
}

测试图标样例:SVG\CustomPackIcon.json

代码语言:json
复制
{
	"JogMotionMode": "M514.829442 971.96488c-13.113669 0-25.545816-5.195326-35.031864-14.634302L372.123252 854.516958c-9.530051-9.501398-14.778588-21.99392-14.778588-35.175127 0-27.526937 22.395056-49.921993 49.921993-49.921993l44.10859 0L451.375248 570.301847 253.599835 570.301847l0 47.591925c0 27.526937-22.395056 49.921993-49.921993 49.921993-13.163811 0-25.640983-5.234211-35.139312-14.740726L57.574277 543.413454c-9.578146-9.550517-14.826684-22.042016-14.826684-35.223223 0-13.180184 5.247514-25.671683 14.777565-35.175127l111.032816-110.234637c9.448186-9.474792 21.940708-14.722306 35.120892-14.722306 27.526937 0 49.921993 22.395056 49.921993 49.921993l0 48.752354 196.614983 0L450.215842 246.961648l-42.948161 0c-27.526937 0-49.921993-22.395056-49.921993-49.921993 0-13.180184 5.247514-25.671683 14.777565-35.175127l0.672312-0.63138 107.232257-94.791923c9.455349-9.295714 21.793352-14.407128 34.802644-14.407128 12.966313 0 25.266453 5.078669 34.710546 14.3181l110.052488 94.298689 0.410346 0.409322c9.530051 9.502421 14.776542 21.994943 14.776542 35.175127 0 27.488051-22.333658 49.858548-49.808406 49.921993L578.427922 246.763127l0 198.808952 197.631126 0 0-45.270043c0-27.526937 22.395056-49.921993 49.921993-49.921993 13.180184 0 25.671683 5.247514 35.174104 14.777565l0.113587 0.11461 105.275695 107.813494c9.485025 9.494235 14.706957 21.957081 14.706957 35.105542 0 13.120832-5.200442 25.560142-14.648628 35.046191L861.155145 653.0382c-9.502421 9.530051-21.99392 14.776542-35.174104 14.776542-27.526937 0-49.921993-22.395056-49.921993-49.921993L776.059048 570.301847 578.427922 570.301847l0 199.11799 46.430472 0c27.526937 0 49.921993 22.395056 49.921993 49.921993 0 13.181207-5.248537 25.673729-14.777565 35.175127l-0.363274 0.352017L549.793768 957.397093C540.316929 966.794114 527.912411 971.96488 514.829442 971.96488zM393.69557 832.673463 501.392408 935.509596c3.701299 3.711532 8.473999 5.756097 13.438057 5.756097 4.963035 0 9.735735-2.044566 13.437034-5.756097l0.394996-0.382717 109.820198-102.505605c3.612271-3.677762 5.599532-8.384971 5.599532-13.280468 0-10.420327-8.803503-19.222807-19.222807-19.222807L547.728736 800.118 547.728736 539.602661l259.029498 0 0 78.291111c0 10.420327 8.803503 19.222807 19.222807 19.222807 4.924149 0 9.658987-2.01182 13.348006-5.666046l105.468077-109.823268c3.711532-3.700275 5.755074-8.471952 5.755074-13.43601s-2.043542-9.736758-5.755074-13.437034l-0.144286-0.145309L839.364863 386.782114c-3.693112-3.678786-8.444323-5.702885-13.383822-5.702885-10.420327 0-19.222807 8.803503-19.222807 19.222807l0 75.969229L547.728736 476.271264 547.728736 216.460983l77.129658-1.00284c10.420327 0 19.222807-8.803503 19.222807-19.222807 0-4.805446-1.915629-9.431813-5.405103-13.0799L528.691147 88.915076l-0.424672-0.426719c-3.701299-3.711532-8.472975-5.756097-13.437034-5.756097s-9.736758 2.044566-13.438057 5.756097l-0.703011 0.661056-107.169835 94.736665c-3.533476 3.659343-5.475711 8.314363-5.475711 13.152555 0 10.419304 8.803503 19.222807 19.222807 19.222807l73.647347 0 0 261.169231L222.900649 477.43067l0-79.45154c0-10.419304-8.80248-19.222807-19.222807-19.222807-4.964058 0-9.735735 2.044566-13.437034 5.755074L79.178317 494.777757c-3.687996 3.676739-5.731538 8.449439-5.731538 13.412474s2.044566 9.735735 5.755074 13.43601l111.038956 109.73424c3.701299 3.711532 8.472975 5.755074 13.437034 5.755074 10.420327 0 19.222807-8.803503 19.222807-19.222807L222.900649 539.602661l259.173784 0 0 260.516362-74.807776 0c-10.419304 0-19.222807 8.803503-19.222807 19.222807C388.04385 824.25984 390.04953 828.988538 393.69557 832.673463z",
	"Calibrate": "M525.897143 0a31.451429 31.451429 0 0 1 31.451428 31.451429v14.628571a459.776 459.776 0 0 1 418.816 442.221714h16.310858a31.451429 31.451429 0 0 1 0 63.122286h-18.578286a459.922286 459.922286 0 0 1-416.475429 409.307429v31.744a31.451429 31.451429 0 0 1-63.049143 0v-30.573715a459.849143 459.849143 0 0 1-433.737142-410.550857H31.451429a31.451429 31.451429 0 0 1 0-62.976l26.843428-0.073143 0.073143-1.755428A459.702857 459.702857 0 0 1 494.372571 44.836571V31.524571A31.451429 31.451429 0 0 1 525.824 0z m-31.597714 119.954286a384.731429 384.731429 0 0 0-360.886858 368.347428h118.637715a31.451429 31.451429 0 0 1 0 63.122286H136.045714a384.877714 384.877714 0 0 0 358.253715 335.433143l0.073142-114.907429a31.451429 31.451429 0 1 1 63.049143 0v113.517715a384.950857 384.950857 0 0 0 340.992-334.043429h-126.537143a31.451429 31.451429 0 0 1 0-63.049143l129.170286-0.073143v-0.731428a384.731429 384.731429 0 0 0-343.625143-366.226286v130.706286a31.451429 31.451429 0 0 1-63.049143 0V119.954286zM512 426.569143a85.357714 85.357714 0 1 1 0 170.715428 85.357714 85.357714 0 0 1 0-170.715428z m-222.72-55.296a15.725714 15.725714 0 0 1 6.363429 21.357714c-5.632 10.386286-10.459429 21.211429-14.336 32.329143a15.798857 15.798857 0 0 1-29.696-10.532571c4.388571-12.580571 9.947429-24.941714 16.310857-36.717715a15.725714 15.725714 0 0 1 21.357714-6.436571zM451.291429 258.486857a15.725714 15.725714 0 0 1-11.117715 19.309714c-36.205714 9.874286-69.924571 28.598857-97.572571 54.272a15.652571 15.652571 0 0 1-22.308572-0.804571 15.798857 15.798857 0 0 1 0.877715-22.308571 265.947429 265.947429 0 0 1 110.738285-61.513143 15.725714 15.725714 0 0 1 19.382858 11.044571z",
	"Drill": "M371.2 130.816l29.952 8.96 208.64 62.72 8.704 2.56c10.496 3.072 21.504-2.816 24.832-13.312 3.072-10.496-2.816-21.504-13.312-24.832l-20.224-6.144V17.92h-208.64v80.384l-18.688-5.632c-10.496-3.072-21.504 2.816-24.832 13.312-2.816 10.496 3.072 21.76 13.568 24.832zM382.72 390.912c-10.496-3.072-21.504 2.816-24.832 13.312-3.072 10.496 2.816 21.504 13.312 24.832l29.952 8.96 208.64 62.72 8.704 2.56c10.496 3.072 21.504-2.816 24.832-13.312 3.072-10.496-2.816-21.504-13.312-24.832l-20.224-6.144V337.92l8.704 2.56c10.496 3.072 21.504-2.816 24.832-13.312 3.072-10.496-2.816-21.504-13.312-24.832l-20.224-6.144-208.64-62.72-18.688-5.632c-10.496-3.072-21.504 2.816-24.832 13.312-3.072 10.496 2.816 21.504 13.312 24.832l29.952 8.96v121.088l-18.176-5.12zM371.2 564.48l29.952 8.96v97.536l78.336 101.632c8.96 11.52 25.856 12.288 35.584 1.536l94.72-103.168v-34.816l8.704 2.56c10.496 3.072 21.504-2.816 24.832-13.312 3.072-10.496-2.816-21.504-13.312-24.832l-20.224-6.144-208.64-62.72-18.688-5.632c-10.496-3.072-21.504 2.816-24.832 13.312-2.816 10.752 3.072 21.76 13.568 25.088zM715.008 736.256s-1.28 8.192-4.096 22.272c-5.376 27.904-8.704 61.696-36.864 106.752-25.344 40.704-103.168 82.176-167.68 82.176-64 0-116.736-52.48-136.704-70.4-19.968-18.176-39.68-73.984-48.896-99.328-8.96-25.344-14.336-35.328-14.336-35.328H120.832L120.32 1003.52l809.472-3.584v-263.68h-214.784z",
	"ParkSpot": "M512 0l6.912 0.042667c227.2 3.712 410.282667 189.013333 410.282667 417.152 0 89.514667-9.472 151.68-66.389334 246.528-56.874667 94.805333-189.610667 246.485333-303.36 341.333333-19.413333 16.128-37.973333 18.944-47.445333 18.944-9.472 0-28.032-2.816-47.402667-18.944-113.792-94.848-246.528-246.528-303.402666-341.333333-56.917333-94.848-66.389333-157.013333-66.389334-246.528C94.805333 186.794667 281.6 0 512 0z m0 75.861333a341.333333 341.333333 0 0 0-6.272 682.581334l6.272 0.085333a341.333333 341.333333 0 0 0 0-682.666667z m-94.805333 132.693334h132.608v75.904h-94.72v151.68h94.72V512h-94.72v113.664a37.888 37.888 0 1 1-75.818667 0V246.613333c0-10.538667 4.224-20.053333 11.093333-26.88a37.546667 37.546667 0 0 1 26.837334-11.136z m132.693333 0a151.722667 151.722667 0 1 1 0 303.445333v-75.861333a75.861333 75.861333 0 0 0 0-151.68V208.597333z"
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档