功能更新
This commit is contained in:
102
Melskin/Markup/EnumSourceExtension.cs
Normal file
102
Melskin/Markup/EnumSourceExtension.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace Melskin.Markup
|
||||
{
|
||||
/// <summary>
|
||||
/// 用于在XAML中提供枚举类型的值源扩展。此扩展允许开发者直接将枚举类型作为数据源,适用于如ComboBox等控件的ItemsSource绑定。
|
||||
/// </summary>
|
||||
public class EnumSourceExtension : MarkupExtension
|
||||
{
|
||||
private Type? enumType;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置一个值,指示是否将枚举项绑定到其 DescriptionAttribute 的描述。如果设置为 true,则返回的枚举项将包含描述信息,并且只能通过 SelectedValue 来绑定源属性。
|
||||
/// </summary>
|
||||
/// <remarks>当此属性设置为 true 时,提供给 UI 的数据源将会是带有 Value 和 Description 属性的对象列表。其中 Value 对应于枚举值,而 Description 则是从枚举成员上的 DescriptionAttribute 中获取的描述文本。</remarks>
|
||||
public bool BindToDescription { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置用作数据源的枚举类型。此属性允许开发者指定一个枚举类型,该类型将作为控件(如ComboBox)的数据源。
|
||||
/// </summary>
|
||||
/// <remarks>当设置了此属性后,对应的控件会使用指定的枚举类型的所有成员作为其数据项。如果需要展示枚举成员上的描述信息,请同时设置 BindToDescription 属性为 true,并通过 SelectedValue 绑定源属性。</remarks>
|
||||
public Type? EnumType
|
||||
{
|
||||
get => enumType;
|
||||
set
|
||||
{
|
||||
if (value == enumType) return;
|
||||
if (null != value)
|
||||
{
|
||||
var type = Nullable.GetUnderlyingType(value) ?? value;
|
||||
if (!type.IsEnum)
|
||||
{
|
||||
throw new ArgumentException("类型必须是枚举类型");
|
||||
}
|
||||
}
|
||||
enumType = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 用Markup语法,绑定枚举类型
|
||||
/// </summary>
|
||||
/// <example><c>ItemsSource="{controls:EnumSource EnumType=controls:Sex}"</c></example>
|
||||
public EnumSourceExtension()
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 用Markup语法,绑定枚举类型
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <remarks>若需要绑定Description,则需设置BindToDescription为True,同时只能用SelectedValue来绑定源属性</remarks>
|
||||
/// <example><c>ItemsSource="{controls:EnumSource controls:ExampleEnum}"</c></example>
|
||||
public EnumSourceExtension(Type enumType) { EnumType = enumType; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object? ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (serviceProvider.GetService(typeof(IProvideValueTarget)) is not IProvideValueTarget pvt)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//如果为空,则返回运行时绑定的类。
|
||||
if (pvt.TargetObject is not FrameworkElement) { return this; }
|
||||
|
||||
if (enumType == null) return null;
|
||||
var enumValues = Enum.GetValues(enumType);
|
||||
if (!BindToDescription) return enumValues;
|
||||
var items = new List<object>();
|
||||
foreach (var enumValue in enumValues)
|
||||
{
|
||||
var item = new
|
||||
{
|
||||
Value = enumValue,
|
||||
Description = GetDescription(enumValue)
|
||||
};
|
||||
items.Add(item);
|
||||
}
|
||||
|
||||
if (pvt.TargetObject is Selector selector)
|
||||
{
|
||||
selector.DisplayMemberPath = "Description";
|
||||
selector.SelectedValuePath = "Value";
|
||||
}
|
||||
|
||||
return items;
|
||||
|
||||
}
|
||||
private static string? GetDescription(object? enumValue)
|
||||
{
|
||||
if (enumValue == null) return string.Empty;
|
||||
var fieldInfo = enumValue.GetType().GetField(enumValue.ToString() ?? string.Empty);
|
||||
var descriptionAttribute = fieldInfo?.GetCustomAttribute<DescriptionAttribute>();
|
||||
|
||||
return descriptionAttribute?.Description ?? enumValue.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
404
Melskin/Markup/IconExtension.cs
Normal file
404
Melskin/Markup/IconExtension.cs
Normal file
@@ -0,0 +1,404 @@
|
||||
using System.Windows.Markup;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Xaml;
|
||||
using Melskin.Appearance;
|
||||
using Melskin.Assets;
|
||||
using Melskin.Controls;
|
||||
|
||||
namespace Melskin.Markup
|
||||
{
|
||||
/// <summary>
|
||||
/// 方便在 XAML 中内联创建 IconElement 的标记扩展。
|
||||
/// 用法示例:
|
||||
/// <code lang="xaml">
|
||||
/// xmlns:neo="clr-namespace:Melskin.Markup;assembly=Melskin"
|
||||
/// <!-- MaterialSymbol -->
|
||||
/// <Button Content="{neo:Icon Search Size=16}"/>
|
||||
/// <!-- 指定 Glyph -->
|
||||
/// <Button Content="{neo:Icon Glyph= FontFamily=/Melskin;component/Assets/Fonts/#YourFont Size=16}"/>
|
||||
/// <!-- 几何 -->
|
||||
/// <Button Content="{neo:Icon Geo=M0,0 L10,0 10,10 0,10Z GeometryFill=DodgerBlue Size=16}"/>
|
||||
/// <!-- 图像 -->
|
||||
/// <Button Content="{neo:Icon Image=/Melskin;component/Assets/Images/add.png Size=20}"/>
|
||||
/// <!-- 资源中的 DrawingBrush -->
|
||||
/// <Button Content="{neo:Icon BrushKey=SomeDrawingBrush Size=20}"/>
|
||||
/// </code>
|
||||
/// </summary>
|
||||
[MarkupExtensionReturnType(typeof(IconElement))]
|
||||
public class IconExtension : MarkupExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置图标符号的字符串表示形式。此属性用于指定要显示的图标的名称。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以使用一个字符串来标识所需的图标。如果设置了此属性且<see cref="Symbol"/>未被设置,则会尝试将这个字符串解析为有效的图标符号。
|
||||
/// </remarks>
|
||||
public string? Symbol { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标的值。此属性用于直接指定图标的具体值。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以直接指定一个具体的图标值。如果<see cref="SymbolValue"/>被设置,则会优先使用这个值来显示图标,而不是通过字符串解析的方式。
|
||||
/// </remarks>
|
||||
public MaterialSymbol? SymbolValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标的字符表示。此属性用于指定要显示的图标的具体字符。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以使用一个字符来标识所需的图标。这个字符通常是从特定字体中选择的一个图标符号,例如BoxIcons等图标字体中的某个字符。
|
||||
/// </remarks>
|
||||
public string? Glyph { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标图像的源。此属性用于指定要显示的图像的位置。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以使用一个字符串来标识图像的来源路径。如果设置了此属性且其他与图像相关的属性未被设置,则会尝试将这个字符串解析为有效的图像源,并加载相应的图像。
|
||||
/// </remarks>
|
||||
public string? Image { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置几何图形的字符串表示形式。此属性用于指定要显示的几何图形的数据。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以使用一个字符串来定义所需的几何图形。如果设置了此属性且字符串有效,则会尝试将这个字符串解析为几何图形数据并应用到图标元素上。
|
||||
/// </remarks>
|
||||
public string? Geo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置用于查找绘图资源的键。此属性允许通过指定的键从资源中检索绘图对象。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 当设置了此属性且提供的键有效时,将尝试从当前资源上下文中查找对应的绘图资源,并将其应用于图标元素。如果找到了与键匹配的绘图资源,则该资源会被用作图标的内容。
|
||||
/// </remarks>
|
||||
public string? DrawingKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置用于查找画刷资源的键。此属性允许通过资源字典中的键来指定一个画刷或绘图画刷。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以使用一个字符串作为键来引用应用程序资源中的画刷对象(<see cref="Brush"/>)或绘图画刷对象(<see cref="DrawingBrush"/>)。如果设置了此属性,并且在资源字典中找到了对应的资源,则会将该资源应用到图标元素上。
|
||||
/// </remarks>
|
||||
public string? BrushKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标的大小。此属性用于指定图标在显示时的尺寸。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以控制图标元素的高度和宽度。如果设置了<see cref="Size"/>属性而未明确设置<see cref="Height"/>和<see cref="Width"/>,则<see cref="Height"/>和<see cref="Width"/>将默认采用<see cref="Size"/>的值。
|
||||
/// </remarks>
|
||||
public double Size { get; set; } = double.NaN;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标的宽度。此属性用于指定图标元素的宽度。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以自定义图标元素的宽度。如果设置了此属性且没有设置<see cref="Size"/>属性,则会直接使用这个值作为图标的宽度。如果同时设置了<see cref="Size"/>和<see cref="Width"/>,则<see cref="Width"/>优先级更高。
|
||||
/// </remarks>
|
||||
public double Width { get; set; } = double.NaN;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标的高度。此属性用于指定图标在显示时的高度。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以控制图标元素的高度。如果设置了<see cref="Size"/>属性而未明确设置<see cref="Height"/>,则<see cref="Height"/>将默认采用<see cref="Size"/>的值。
|
||||
/// </remarks>
|
||||
public double Height { get; set; } = double.NaN;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标的前景色。此属性用于指定图标中文字或几何图形部分的颜色。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以自定义图标的颜色。如果未显式设置此属性,则可能使用默认值或其他相关属性来确定颜色。
|
||||
/// </remarks>
|
||||
public Brush? Foreground { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置几何图形填充的画刷。此属性用于指定图标中几何图形部分的填充颜色。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以自定义图标的几何图形部分的填充效果。如果未显式设置此属性,则可能使用默认值或其他相关属性(如<see cref="Foreground"/>)来确定填充颜色。
|
||||
/// </remarks>
|
||||
public Brush? GeometryFill { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置几何图形描边的颜色。此属性用于指定图标的几何形状边缘所使用的画刷。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以自定义图标中几何形状边缘的颜色。如果设置了此属性,则会使用指定的画刷来绘制几何形状的边缘。
|
||||
/// </remarks>
|
||||
public Brush? GeometryStroke { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置几何图形描边的厚度。此属性用于控制图标的几何形状描边宽度。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以自定义图标中几何图形描边的宽度。如果设置了此属性且值不是NaN,则会应用指定的描边厚度。
|
||||
/// </remarks>
|
||||
public double GeometryStrokeThickness { get; set; } = double.NaN;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标内容的类型。此属性用于指定图标的显示方式。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以定义图标是作为材料图标、字形、图像、几何图形、绘图还是画刷来展示。默认值为Auto,表示自动选择最合适的内容类型。
|
||||
/// </remarks>
|
||||
public IconElement.IconContentType ContentType { get; set; } = IconElement.IconContentType.Auto;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标的字体系列。此属性用于指定图标所使用的字体,以便正确显示图标符号。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以指定一个字体系列来确保图标能够以正确的样式显示。如果设置了此属性,则会应用到图标元素的字体系列上。
|
||||
/// </remarks>
|
||||
public FontFamily? FontFamily { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标的字体大小。此属性用于控制图标显示时的字体大小。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以指定图标字体的具体大小。如果设置了此属性且未被其他尺寸属性覆盖,则会使用这个值来设置图标的字体大小。
|
||||
/// </remarks>
|
||||
public double FontSize { get; set; } = double.NaN;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图像的拉伸模式。此属性决定了图像如何填充其布局空间。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以控制图像在显示时的拉伸方式。默认值为 <see cref="Stretch.Uniform"/>,表示图像将按比例缩放以适应可用空间,同时保持其原始宽高比。
|
||||
/// </remarks>
|
||||
public Stretch ImageStretch { get; set; } = Stretch.Uniform;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标元素的边距。此属性用于控制图标与其容器之间的空间。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以自定义图标在布局中的位置和间距。边距值是一个<see cref="Thickness"/>类型的对象,允许指定左、上、右、下四个方向的边距。
|
||||
/// </remarks>
|
||||
public Thickness Margin { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标元素的水平对齐方式。此属性用于指定图标在其容器中的水平位置。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以控制图标在水平方向上的对齐方式。默认值为居中对齐(HorizontalAlignment.Center)。
|
||||
/// </remarks>
|
||||
public HorizontalAlignment HorizontalAlignment { get; set; } = HorizontalAlignment.Center;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置图标在垂直方向上的对齐方式。此属性用于控制图标在其容器中的垂直位置。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 通过设置此属性,可以指定图标在其容器中是顶部对齐、居中对齐还是底部对齐。默认情况下,图标是居中对齐的。
|
||||
/// </remarks>
|
||||
public VerticalAlignment VerticalAlignment { get; set; } = VerticalAlignment.Center;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在XAML中创建图标元素的标记扩展。通过设置不同的属性,可以自定义图标的样式和内容。
|
||||
/// </summary>
|
||||
public IconExtension()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于在XAML中创建图标元素的标记扩展。通过设置不同的属性,可以自定义图标的样式、内容和外观。
|
||||
/// </summary>
|
||||
public IconExtension(string symbol)
|
||||
{
|
||||
Symbol = symbol;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于在XAML中创建图标元素的标记扩展。通过设置不同的属性,可以自定义图标的样式、内容和外观。
|
||||
/// </summary>
|
||||
public IconExtension(MaterialSymbol materialSymbol)
|
||||
{
|
||||
SymbolValue = materialSymbol;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
var icon = new IconElement();
|
||||
|
||||
// 尺寸
|
||||
if (!double.IsNaN(Size))
|
||||
{
|
||||
if (double.IsNaN(Width)) icon.Width = Size;
|
||||
if (double.IsNaN(Height)) icon.Height = Size;
|
||||
if (double.IsNaN(FontSize)) FontSize = Size;
|
||||
}
|
||||
if (!double.IsNaN(Width)) icon.Width = Width;
|
||||
if (!double.IsNaN(Height)) icon.Height = Height;
|
||||
|
||||
if (!double.IsNaN(FontSize)) icon.SetValue(Control.FontSizeProperty, FontSize);
|
||||
if (FontFamily != null) icon.SetValue(Control.FontFamilyProperty, FontFamily);
|
||||
if (Margin != default) icon.Margin = Margin;
|
||||
|
||||
icon.HorizontalAlignment = HorizontalAlignment;
|
||||
icon.VerticalAlignment = VerticalAlignment;
|
||||
|
||||
if (Foreground != null)
|
||||
icon.SetValue(Control.ForegroundProperty, Foreground);
|
||||
|
||||
// 上下文(已移除元组)
|
||||
var ctx = GetContext(serviceProvider);
|
||||
var resourceOwner = ctx.RootObject as FrameworkElement ?? ctx.TargetObject as FrameworkElement;
|
||||
|
||||
// Drawing
|
||||
if (!string.IsNullOrEmpty(DrawingKey))
|
||||
{
|
||||
var drawingObj = TryFindResource(resourceOwner, DrawingKey);
|
||||
if (drawingObj is Drawing d)
|
||||
{
|
||||
icon.Drawing = d;
|
||||
}
|
||||
}
|
||||
|
||||
// DrawingBrush / Brush
|
||||
if (!string.IsNullOrEmpty(BrushKey))
|
||||
{
|
||||
var brushObj = TryFindResource(resourceOwner, BrushKey);
|
||||
switch (brushObj)
|
||||
{
|
||||
case DrawingBrush db:
|
||||
icon.DrawingBrush = db;
|
||||
break;
|
||||
case Brush b:
|
||||
GeometryFill ??= b;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Geometry
|
||||
if (!string.IsNullOrEmpty(Geo))
|
||||
{
|
||||
try
|
||||
{
|
||||
var g = Geometry.Parse(Geo);
|
||||
if (g.CanFreeze) g.Freeze();
|
||||
icon.Geometry = g;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
// Image
|
||||
if (!string.IsNullOrEmpty(Image))
|
||||
{
|
||||
try
|
||||
{
|
||||
var uri = TryCreateUri(Image);
|
||||
if (uri != null)
|
||||
{
|
||||
var bmp = new BitmapImage();
|
||||
bmp.BeginInit();
|
||||
bmp.UriSource = uri;
|
||||
bmp.CacheOption = BitmapCacheOption.OnLoad;
|
||||
bmp.EndInit();
|
||||
if (bmp.CanFreeze) bmp.Freeze();
|
||||
icon.ImageSource = bmp;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
// Glyph
|
||||
if (!string.IsNullOrEmpty(Glyph))
|
||||
icon.Glyph = Glyph;
|
||||
|
||||
// Symbol
|
||||
if (SymbolValue.HasValue)
|
||||
icon.Symbol = SymbolValue.Value;
|
||||
else if (!string.IsNullOrEmpty(Symbol))
|
||||
icon.Symbol = IconElement.Parse(Symbol);
|
||||
|
||||
// ContentType
|
||||
if (ContentType != IconElement.IconContentType.Auto)
|
||||
icon.ContentType = ContentType;
|
||||
|
||||
// Geometry 样式
|
||||
if (icon.Geometry != null)
|
||||
{
|
||||
if (GeometryFill != null)
|
||||
icon.GeometryFill = GeometryFill;
|
||||
else if (Foreground != null && icon.GeometryFill == System.Windows.Media.Brushes.Black)
|
||||
icon.GeometryFill = Foreground;
|
||||
|
||||
if (GeometryStroke != null)
|
||||
icon.GeometryStroke = GeometryStroke;
|
||||
|
||||
if (!double.IsNaN(GeometryStrokeThickness))
|
||||
icon.GeometryStrokeThickness = GeometryStrokeThickness;
|
||||
}
|
||||
|
||||
icon.ImageStretch = ImageStretch;
|
||||
return icon;
|
||||
}
|
||||
|
||||
private sealed class ProvideContext
|
||||
{
|
||||
public object? TargetObject { get; set; }
|
||||
public object? TargetProperty { get; set; }
|
||||
public object? RootObject { get; set; }
|
||||
}
|
||||
|
||||
private static ProvideContext GetContext(IServiceProvider serviceProvider)
|
||||
{
|
||||
object? targetObject = null;
|
||||
object? targetProperty = null;
|
||||
object? rootObject = null;
|
||||
|
||||
var pvt = serviceProvider?.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
|
||||
if (pvt != null)
|
||||
{
|
||||
targetObject = pvt.TargetObject;
|
||||
targetProperty = pvt.TargetProperty;
|
||||
}
|
||||
|
||||
var rop = serviceProvider?.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
|
||||
if (rop != null)
|
||||
{
|
||||
rootObject = rop.RootObject;
|
||||
}
|
||||
|
||||
return new ProvideContext
|
||||
{
|
||||
TargetObject = targetObject,
|
||||
TargetProperty = targetProperty,
|
||||
RootObject = rootObject
|
||||
};
|
||||
}
|
||||
|
||||
private static object? TryFindResource(FrameworkElement? fe, string? key)
|
||||
{
|
||||
if (fe == null||key == null) return null;
|
||||
var found = fe?.TryFindResource(key);
|
||||
if (found != null) return found;
|
||||
if (Application.Current != null && Application.Current.Resources.Contains(key))
|
||||
return Application.Current.Resources[key];
|
||||
else
|
||||
{
|
||||
return ThemeManager.Current?.TryFindResource(key);
|
||||
}
|
||||
//return null;
|
||||
}
|
||||
|
||||
private static Uri? TryCreateUri(string? path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path)) return null;
|
||||
if (path!.IndexOf(";component/", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
path.StartsWith("pack://", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (!path.StartsWith("pack://", StringComparison.OrdinalIgnoreCase))
|
||||
path = "pack://application:,,," + (path.StartsWith("/") ? path : "/" + path);
|
||||
return new Uri(path, UriKind.Absolute);
|
||||
}
|
||||
return Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out var u) ? u : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
76
Melskin/Markup/SymbolIconExtension.cs
Normal file
76
Melskin/Markup/SymbolIconExtension.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
|
||||
|
||||
|
||||
|
||||
using System.Windows.Markup;
|
||||
using Melskin.Assets;
|
||||
|
||||
namespace Melskin.Markup;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在XAML中方便地使用Material Design图标字体的标记扩展。通过指定符号名称,可以轻松地将图标添加到UI元素中。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 使用此扩展时,需要确保已正确引用了Material Design图标字体资源。可以通过设置`FontFamily`属性来应用图标字体。
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// <code lang="xml"> <TextBlock Text="{enu:SymbolIcon Symbol=AddBox}" FontFamily="{StaticResource MaterialIconFont}"/>
|
||||
/// </code>
|
||||
/// </example>
|
||||
[ContentProperty(nameof(Symbol))]
|
||||
[MarkupExtensionReturnType(typeof(string))]
|
||||
public class SymbolIconExtension : MarkupExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 用于在 XAML 中扩展 Material 符号图标的使用范围,允许直接通过字符串指定图标名称并显示对应的符号。
|
||||
/// </summary>
|
||||
public SymbolIconExtension()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于将 Material 符号图标转换为字符串表示形式的标记扩展类。此类主要用于在 XAML 中直接使用 Material 符号。
|
||||
/// </summary>
|
||||
public SymbolIconExtension(MaterialSymbol symbol)
|
||||
{
|
||||
Symbol = symbol; }
|
||||
|
||||
/// <summary>
|
||||
/// SymbolIconExtension 类用于在 XAML 中通过标记扩展来设置 Material 符号图标。该类允许开发者通过字符串参数指定 Material 符号的名称,然后将其转换为对应的枚举值。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 使用时需提供一个有效的 MaterialSymbol 枚举成员名作为构造函数参数。这个扩展可以方便地与 UI 元素结合使用,以动态地设置或绑定图标属性。
|
||||
/// </remarks>
|
||||
public SymbolIconExtension(string symbol)
|
||||
{
|
||||
Symbol = (MaterialSymbol)Enum.Parse(typeof(MaterialSymbol), symbol); }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
return ((char)(int)Symbol).ToString();
|
||||
|
||||
//SymbolIcon symbolIcon = new(Symbol);
|
||||
|
||||
//if (FontSize > 0)
|
||||
//{
|
||||
// symbolIcon.FontSize = FontSize;
|
||||
//}
|
||||
|
||||
//return symbolIcon;
|
||||
}
|
||||
|
||||
|
||||
// public double FontSize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 表示用于图标扩展的符号。此属性允许在XAML中直接指定一个符号来创建相应的图标。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 该属性是<see cref="SymbolIconExtension"/>类的核心,通过设置不同的<see cref="MaterialSymbol"/>值,
|
||||
/// 可以生成不同样式的图标。它支持在XAML标记中使用,使得界面设计更加直观和灵活。
|
||||
/// </remarks>
|
||||
[ConstructorArgument("symbol")]
|
||||
public MaterialSymbol Symbol { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user