Search code examples
c#.netxamlmauimaui-community-toolkit

.NET MAUI Library: XamlC error XFC0000: Cannot Resolve Type


When using Visual Studio to build a .NET MAUI library that contains a reference to the .NET MAUI Community Toolkit's IsEqualConverter, the following error is displayed in the Visual Studio Output window:

XamlC error XFC0000: Cannot resolve type "http://schemas.microsoft.com/dotnet/2022/maui/toolkit:mct:IsEqualConverter".

This issue also occurs when trying to use other .NET MAUI Community Toolkit Converters in a class Library.

Steps To Reproduce

  1. Open the solution from the repro repository in Visual Studio.
  2. Attempt to build the project. If the problem exists, an XamlC error XFC0000: Cannot resolve type error will be reported in the VS Output window for each target platform.

Link to public reproduction project repository

https://github.com/awalker-dsg/MauiLibTestLib_ConverterIssue

<?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"
             x:Class="MauiLibTestLib_ConverterIssue.Views.IsEqualConverterPage"
             xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             Title="IsEqualConverterPage">
    <ContentPage.Resources>
        <x:String x:Key="MAUI">MAUI</x:String>
        <mct:IsEqualConverter x:Key="IsEqualConverter" />        
    </ContentPage.Resources>

    <!-- 
        The code in VerticalStackLayout was taken from the MAUI community toolkit sample code here:
        https://github.com/CommunityToolkit/Maui/blob/main/samples/CommunityToolkit.Maui.Sample/Pages/Converters/IsEqualConverterPage.xaml
    -->
    <VerticalStackLayout Spacing="10">
        <Label Text="The IsEqualConverter is a converter that allows users to convert any value binding to a bool depending on whether or not it is equal to a different value. The initial binding contains the object that will be compared and the ConverterParameter contains the object to compare it to."/>
        <Entry Margin="0"
                    Text="{Binding InputValue}" 
                    HorizontalOptions="FillAndExpand"/>
        <Label Text="Does the entry text equal &quot;MAUI&quot;?"/>
        <Label FontSize="Large" 
                    HorizontalOptions="FillAndExpand"
                    Text="Yes, it does!"
                    TextColor="Green">
            <Label.Triggers>
                <DataTrigger TargetType="Label"
                                    Binding="{Binding InputValue, Converter={StaticResource IsEqualConverter}, ConverterParameter={StaticResource MAUI}}"
                                    Value="False">
                    <Setter Property="Text" Value="No, it doesn't" />
                    <Setter Property="TextColor" Value="Red" />
                </DataTrigger>
            </Label.Triggers>
        </Label>
    </VerticalStackLayout>
</ContentPage>

Solution

  • The linker is removing our library from your project. This happens when the compiler removes "unused" third-party libraries and it happens because the linker isn't intelligent enough to understand that the library is being used in XAML (it only knows how to scan C# code).

    Workaround

    Update your XAML code to add x:Name which will generate a C# property for the Behavior in your code-behind; during compilation, the linker will now see that you are using CommunityToolkit.Maui and will no longer remove it:

    <mct:IsEqualConverter x:Key="IsEqualConverter" x:Name="IsEqualConverter"/>