.NET MAUI库:XamlC错误XFC0000:无法解析类型

wlsrxk51  于 2023-09-28  发布在  .NET
关注(0)|答案(1)|浏览(182)

使用Visual Studio生成包含.NET MAUI Community Toolkit'sIsEqualConverter引用的.NET MAUI库时,Visual Studio“输出”窗口中显示以下错误:
XamlC错误XFC0000:无法解析类型“http://schemas.microsoft.com/dotnet/2022/maui/toolkit:mct:IsEqualConverter“。
尝试在类库中使用其他.NET MAUI Community Toolkit转换器时也会出现此问题。

复制步骤

1.从Visual Studio中的复制存储库打开解决方案。
1.尝试生成项目。如果问题存在,则在每个目标平台的VS输出窗口中将报告XamlC error XFC0000: Cannot resolve type错误。

公共复制项目仓库链接

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>
oewdyzsn

oewdyzsn1#

链接器正在从您的项目中删除我们的库。当编译器删除“未使用的”第三方库时会发生这种情况,这是因为链接器不够智能,无法理解该库正在XAML中使用(它只知道如何扫描C#代码)。

解决方案

更新您的XAML代码以添加x:Name,这将为代码隐藏中的行为生成C#属性;在编译过程中,链接器现在会看到你正在使用CommunityToolkit.Maui,并且不再删除它:

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

相关问题