xamlmauiexpandermaui-community-toolkit

Unable to use CommunityToolkit.MAUI in a project MAUI Class Library


I want to customize the Expander view from the package CommunityToolkit.Maui. After installing the Nuget package CommunityToolkit.Maui 8.0.1, there is no problem to use it in an App project. But if in the same solution I add a MAUI class library project (and install the package CommunityToCommunityToolkit.Maui) I obtain the error:

XFC0000 Cannot resolve type "http://schemas.microsoft.com/dotnet/2022/maui/toolkit:ct:Expander" if I use an Expander. Is it allowed to use CommunityToCommunityToolkit.Maui in a class library project ?

In the App project, this run correctly :

    <?xml version="1.0" encoding="utf-8" ?\>
    <ContentPage xmlns=".....
     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\>

but in the class library project, this don't compile :

   <?xml version="1.0" encoding="utf-8" ?>
     <ContentView xmlns="....
 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


Solution

  • It is likely that the issue is caused by the linker; the linker is striping out unused components, in your case it is considering Expander as unused. Try one of the following:

    <ct:Expander x:name="expander">
    
    _ = new Expander();
    

    Credit/Source:

    https://github.com/CommunityToolkit/Maui/issues/1218#issuecomment-1577568396