Closed Poker-sang closed 2 months ago
写了一个简单的例子可供参考【
/// <summary>
/// An abstract <see cref="MarkupExtension"/> which to produce text-based icons.
/// </summary>
public abstract class SymbolIconBaseExtension : MarkupExtension
{
/// <summary>
/// Gets or sets if the icon is filled.
/// </summary>
public bool IsFilled { get; set; }
/// <summary>
/// Gets or sets if the icon use Segoe metrics.
/// </summary>
public bool UseSegoeMetrics { get; set; }
/// <summary>
/// Gets or sets the size of the icon to display.
/// </summary>
public double FontSize { get; set; }
/// <summary>
/// Gets or sets the symbol of the icon to display.
/// </summary>
public Symbol Symbol { get; set; }
/// <summary>
/// Gets or sets the size of the icon to display. Priority is higher than <see cref="FontSize"/>.
/// </summary>
public FontSizeType Size { get; set; }
/// <summary>
/// Gets or sets the thickness of the icon glyph.
/// </summary>
public FontWeight FontWeight { get; set; } = FontWeights.Normal;
/// <summary>
/// Gets or sets the font style for the icon glyph.
/// </summary>
public FontStyle FontStyle { get; set; } = FontStyle.Normal;
/// <summary>
/// Gets or sets the foreground <see cref="Brush"/> for the icon.
/// </summary>
public Brush? Foreground { get; set; }
/// <summary>
/// Gets or sets a value indicating whether automatic text enlargement, to reflect the system text size setting, is enabled.
/// </summary>
public bool IsTextScaleFactorEnabled { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the icon is mirrored when the flow direction is right to left.
/// </summary>
public bool MirroredWhenRightToLeft { get; set; }
}
/// <summary>
/// Symbol icon source markup extension
/// </summary>
[MarkupExtensionReturnType(ReturnType = typeof(SymbolIconSource))]
public class SymbolIconSourceExtension : SymbolIconBaseExtension
{
/// <inheritdoc />
protected override object ProvideValue()
{
var icon = new SymbolIconSource
{
IsFilled = IsFilled,
UseSegoeMetrics = UseSegoeMetrics,
Symbol = Symbol,
FontWeight = FontWeight,
FontStyle = FontStyle,
IsTextScaleFactorEnabled = IsTextScaleFactorEnabled,
MirroredWhenRightToLeft = MirroredWhenRightToLeft
};
if (Size is not FontSizeType.None)
icon.FontSize = (int)Size;
else if (FontSize > 0)
icon.FontSize = FontSize;
if (Foreground is not null)
icon.Foreground = Foreground;
return icon;
}
}
/// <summary>
/// Symbol icon markup extension
/// </summary>
[MarkupExtensionReturnType(ReturnType = typeof(SymbolIcon))]
public class SymbolIconExtension : SymbolIconBaseExtension
{
/// <inheritdoc cref="FrameworkElement.FlowDirection"/>
public FlowDirection FlowDirection { get; set; }
/// <inheritdoc />
protected override object ProvideValue()
{
var icon = new SymbolIcon
{
IsFilled = IsFilled,
UseSegoeMetrics = UseSegoeMetrics,
Symbol = Symbol,
FontWeight = FontWeight,
FontStyle = FontStyle,
IsTextScaleFactorEnabled = IsTextScaleFactorEnabled,
MirroredWhenRightToLeft = MirroredWhenRightToLeft,
FlowDirection = FlowDirection
};
if (Size is not FontSizeType.None)
icon.FontSize = (int)Size;
else if (FontSize > 0)
icon.FontSize = FontSize;
if (Foreground is not null)
icon.Foreground = Foreground;
return icon;
}
}
/// <summary>
/// Font size
/// </summary>
/// <remarks>
/// <see href="https://learn.microsoft.com/windows/apps/design/style/segoe-fluent-icons-font"/>
/// </remarks>
public enum FontSizeType
{
/// <summary>
/// Use FontSize property
/// </summary>
None = 0,
/// <summary>
/// 16
/// </summary>
Small = 16,
/// <summary>
/// 20
/// </summary>
Normal = 20,
/// <summary>
/// 24
/// </summary>
Large = 24,
/// <summary>
/// 32
/// </summary>
ExLarge = 32,
/// <summary>
/// 48
/// </summary>
ExExLarge = 48,
/// <summary>
/// 64
/// </summary>
ExExExLarge = 64
}
Implemented in 52a57f6516796a0900eb8b2468322e1eed12be17.
烦请测试,若实现有缺漏欢迎 re-open.
@davidxuang 示例代码中Extension可以省略】就像Attribute那样
<Window xmlns:ic="using:FluentIcons.WinUI">
<Expander Header="{ic:SymbolIcon Symbol=ArrowLeft}" />
</Window>
希望可以在 WinUI 等支持标记扩展:
这样在 xaml 里就不需要写嵌套标签了:
标记扩展应该需要支持
Icon
和IconSource
两个版本