You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI 10中使用AvatarView控件报MissingMethodException异常

解决CommunityToolkit.Maui AvatarView缺少默认构造函数异常

异常说明

System.MissingMethodException: CommunityToolkit.Maui.Views.AvatarView缺少默认构造函数
Microsoft.Maui.Controls.Xaml.XamlParseException

该异常是因为CommunityToolkit.Maui 13.0.0版本中,AvatarView移除了无参构造函数,必须通过指定属性初始化控件,XAML解析时找不到无参构造就会触发错误。

修复步骤

  • 修正XAML中AvatarView的声明
    不能仅写空标签<toolkitViews:AvatarView/>,必须指定至少Text或Source属性来初始化,示例:

    <!-- 使用文本初始化 -->
    <toolkitViews:AvatarView Text="AB" BackgroundColor="#2196F3" TextColor="White" />
    <!-- 使用图片源初始化 -->
    <toolkitViews:AvatarView Source="{Binding UserAvatar}" />
    
  • 统一依赖包版本
    确保项目中所有CommunityToolkit相关包版本一致,避免版本不兼容问题。在项目文件(.csproj)中检查并修改:

    <PackageReference Include="CommunityToolkit.Maui" Version="13.0.0" />
    <PackageReference Include="CommunityToolkit.Maui.Core" Version="13.0.0" />
    
  • 清理重建项目
    缓存可能导致XAML解析异常,执行以下操作:

    • 右键项目选择「清理」
    • 手动删除项目目录下的bin和obj文件夹
    • 重新构建项目
  • 验证MauiProgram配置
    确保UseMauiCommunityToolkit()配置位置正确,需在UseMauiApp<App>()之后:

    var builder = MauiApp.CreateBuilder();
    builder
        .UseMauiApp<App>()
        .UseMauiCommunityToolkit()
        .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
        });
    return builder.Build();
    

内容的提问来源于stack exchange,提问作者qqqqqkks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 00:53:08