MAUI类库使用CommunityToolkit.Maui的Expander组件报XFC0000错误
我想自定义CommunityToolkit.Maui包中的Expander视图。安装NuGet包CommunityToolkit.Maui 8.0.1后,MAUI应用项目中使用Expander完全正常,但在同解决方案的MAUI类库项目中安装该包后,使用Expander时出现编译错误:XFC0000 无法解析类型"http://schemas.microsoft.com/dotnet/2022/maui/toolkit:ct:Expander"。请问MAUI类库项目是否支持使用CommunityToolkit.Maui?
应用项目可正常运行的代码:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:ct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" x:Class="MauiAppTest.PageTest"> <ct:Expander> <ct:Expander.Header> <Label Text="Click to expand" /> </ct:Expander.Header> <ct:Expander.Content> <StackLayout> <Label Text="Expanded content goes here" /> <!-- Add more content here as needed --> </StackLayout> </ct:Expander.Content> </ct:Expander> </ContentPage>
类库项目中编译失败的代码:
<?xml version="1.0" encoding="utf-8" ?> <ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:ct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" x:Class="UserCtrlDr.DropDownCtrl"> <ct:Expander> <ct:Expander.Header> <Label Text="Click to expand" /> </ct:Expander.Header> <ct:Expander.Content> <StackLayout> <Label Text="Expanded content goes here" /> <!-- Add more content here as needed --> </StackLayout> </ct:Expander.Content> </ct:Expander> </ContentView>
错误信息:
XFC0000 Cannot resolve type "http://schemas.microsoft.com/dotnet/2022/maui/toolkit:ct:Expander". UserCtrlDr (net8.0-android), UserCtrlDr (net8.0-ios), UserCtrlDr (net8.0-maccatalyst), UserCtrlDr (net8.0-windows10.0.19041.0) D:\Applications\AppliTribunal\Apprentissage MAUI\excercice\MauiAppTest\UserCtrlDr\DropDownCtrl.xaml
MAUI类库项目完全支持使用CommunityToolkit.Maui,出现该错误是因为类库项目的XAML引用方式或初始化环节有遗漏,按以下步骤排查修复:
- 确认类库项目已正确安装CommunityToolkit.Maui 8.0.1 NuGet包,版本需与应用项目保持一致
- 将类库XAML中的命名空间声明替换为clr-namespace方式,直接指向Toolkit的程序集,替换原有的schema地址:
xmlns:ct="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui" - 确保应用项目的
MauiProgram.cs中已添加Toolkit初始化代码:builder.UseMauiApp<App>().UseMauiCommunityToolkit(); - 清理整个解决方案,手动删除类库和应用项目的
bin、obj文件夹,然后重新生成解决方案
内容的提问来源于stack exchange,提问作者Dominique Ristori

