SuperAPI AI 工具集

syncfusion-winui-avatar-view

前端与建站

查看项目

更新于 10-16 02:06

技能介绍

用途

SfAvatarView 是 Syncfusion 提供的 WinUI 控件,用于以图形方式表示用户图像,适用于需要在界面中展示用户头像、个人资料图片或用户标识的场景。当用户提到头像、个人资料图片、用户图像、姓名首字母、联系人图像、用户图标、资料徽章或群组头像时,可使用该控件。

主要能力

控件支持五种内容类型:

  • Default:默认头像
  • Initials:根据姓名生成首字母
  • CustomImage:通过 ImageSource 显示自定义图片
  • AvatarCharacter:使用 25 个预定义头像角色(Avatar1 至 Avatar25)
  • Group:群组视图,最多显示 3 名成员

首字母生成支持两种格式:SingleCharacter(取首字母,如 "Alex" → "A")和 DoubleCharacter(单个词取首尾字母,如 "Alex" → "AX";多个词取前两个词的首字母,如 "John Doe" → "JD")。

外观方面,AvatarShape 支持 Circle、Square 和 Custom(配合 CornerRadius);AvatarSize 提供 ExtraSmall、Small、Medium、Large、ExtraLarge 五档预设尺寸。还可自定义边框(BorderBrush、BorderThickness)、背景(纯色或 LinearGradientBrush、RadialGradientBrush 渐变)、字体(FontFamily、Foreground、FontSize)以及主题集成。

群组视图通过 GroupSource 绑定集合,并可通过 InitialsMemberPath、ImageSourceMemberPath、BackgroundColorMemberPath、InitialsColorMemberPath 指定成员属性路径。

使用方式

命名空间:Syncfusion.UI.Xaml.Core NuGet 包:Syncfusion.Core.WinUI 平台:WinUI 3 Desktop(.NET 5+、Windows App SDK 1.0+)

需进行许可证注册。基本 XAML 用法示例:

<Page
    xmlns:syncfusion="using:Syncfusion.UI.Xaml.Core">
    <Grid>
        <syncfusion:SfAvatarView 
            x:Name="userAvatar"
            ContentType="Initials"
            AvatarName="John Doe"
            InitialsType="DoubleCharacter"
            AvatarSize="Large"
            Background="CornflowerBlue"
            Foreground="White"
            AvatarShape="Circle"/>
    </Grid>
</Page>

C# 用法示例:

using Syncfusion.UI.Xaml.Core;
using Microsoft.UI;

SfAvatarView avatarView = new SfAvatarView
{
    ContentType = AvatarContentType.Initials,
    AvatarName = "John Doe",
    InitialsType = AvatarInitialsType.DoubleCharacter,
    AvatarSize = AvatarSize.Large,
    Background = new SolidColorBrush(Colors.CornflowerBlue),
    Foreground = new SolidColorBrush(Colors.White),
    AvatarShape = AvatarShape.Circle
};

this.Content = avatarView;

适用场景

  • 用户资料:资料页、账户设置、用户仪表盘、成员资料。建议优先使用 CustomImage,无图片时回退到 Initials。
  • 联系人列表:电话联系人、电子邮件联系人、团队成员目录、组织架构图。建议使用圆形、Medium 或 Small 尺寸。
  • 聊天/消息:聊天会话、消息线程、评论区、论坛帖子。建议使用 Small 或 Medium 尺寸,配合 Initials 或 AvatarCharacter 快速加载。
  • 群组展示:团队协作视图、群聊参与者、项目成员、共享文档贡献者。使用 Group 内容类型显示最多 3 名参与者。
  • 应用导航:用户菜单、导航栏、抽屉导航、应用栏中的用户头像。建议使用 Small 或 ExtraSmall 尺寸。

限制与注意事项

  • 群组视图最多支持 3 个项目,若 GroupSource 中项目更多,仅显示前 3 个。
  • 图片加载使用 BitmapImage,支持 ms-appx:/// 或 https:// URI,需妥善处理加载错误。
  • 首字母渲染比图片更快,长列表建议使用首字母,详情视图使用自定义图片。
  • 头像图片应设置 AutomationProperties.Name 为用户姓名以支持屏幕阅读器。
  • 可使用 ThemeResource 使背景自动适配明暗主题,例如 Background="{ThemeResource SystemAccentColor}"。
  • 建议缓存头像图片以避免重复下载。
  • 尺寸参考:ExtraSmall(32x32)用于导航栏和紧凑列表;Small(48x48)用于聊天消息和密集列表;Medium(64x64)用于标准列表和卡片;Large(96x96)用于资料页和突出展示;ExtraLarge(128x128)用于大型资料头部和焦点区域。

关键属性

  • ContentType(AvatarContentType,默认 Default):内容类型。
  • ImageSource(ImageSource,默认 null):自定义图片。
  • AvatarName(string,默认 null):用于生成首字母的姓名。
  • AvatarCharacter(AvatarCharacter,默认 Avatar1):预定义头像角色。
  • InitialsType(AvatarInitialsType,默认 SingleCharacter):首字母格式。
  • AvatarShape(AvatarShape,默认 Circle):形状。
  • AvatarSize(AvatarSize,默认 Small):尺寸。
  • GroupSource(IEnumerable,默认 null):群组集合,最多 3 项。
  • InitialsMemberPath、ImageSourceMemberPath、BackgroundColorMemberPath、InitialsColorMemberPath(string,默认 null):群组成员属性路径。
  • Background(Brush,默认 Gray):背景色或渐变。
  • Foreground(Brush,默认 Black):首字母文字颜色。
  • BorderBrush(Brush,默认 null):边框颜色。
  • BorderThickness(Thickness,默认 0):边框粗细。
  • CornerRadius(CornerRadius,默认 Auto):自定义形状的圆角半径。
  • FontFamily(FontFamily,默认 Segoe UI):首字母字体。
  • FontSize(double,默认 Auto):首字母字号,根据 AvatarSize 自动计算。