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

huangapple go评论119阅读模式
英文:

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

问题

使用Visual Studio构建一个包含对.NET MAUI Community ToolkitIsEqualConverter引用的.NET MAUI库时,在Visual Studio输出窗口中会显示以下错误:

> XamlC错误 XFC0000:无法解析类型“http://schemas.microsoft.com/dotnet/2022/maui/toolkit:mct:IsEqualConverter”。

在类库中尝试使用其他.NET MAUI Community Toolkit转换器时也会出现此问题。

复现步骤

  1. 在Visual Studio中打开复制存储库中的解决方案。
  2. 尝试构建项目。如果存在问题,每个目标平台的VS输出窗口将报告“XamlC错误XFC0000:无法解析类型”错误。

公共复制存储库链接

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>

    <!-- 
        VerticalStackLayout中的代码来自MAUI社区工具包示例代码:
        https://github.com/CommunityToolkit/Maui/blob/main/samples/CommunityToolkit.Maui.Sample/Pages/Converters/IsEqualConverterPage.xaml
    -->
    <VerticalStackLayout Spacing="10">
        <Label Text="IsEqualConverter是一个转换器,允许用户将任何值绑定到布尔值,具体取决于它是否等于不同的值。初始绑定包含将进行比较的对象,ConverterParameter包含要与之进行比较的对象。"/>
        <Entry Margin="0"
                    Text="{Binding InputValue}" 
                    HorizontalOptions="FillAndExpand"/>
        <Label Text="输入文本是否等于“MAUI”?"/>
        <Label FontSize="Large" 
                    HorizontalOptions="FillAndExpand"
                    Text="是的,它是!"
                    TextColor="Green">
            <Label.Triggers>
                <DataTrigger TargetType="Label"
                                    Binding="{Binding InputValue, Converter={StaticResource IsEqualConverter}, ConverterParameter={StaticResource MAUI}}"
                                    Value="False">
                    <Setter Property="Text" Value="不,它不是" />
                    <Setter Property="TextColor" Value="Red" />
                </DataTrigger>
            </Label.Triggers>
        </Label>
    </VerticalStackLayout>
</ContentPage>
英文:

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.

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 "MAUI"?"/>
        <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>

答案1

得分: 6

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

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

解决方法

更新您的 XAML 代码,添加 x:Name,这将在您的代码后端生成一个 C# 属性,用于行为;在编译期间,链接器现在将看到您正在使用 CommunityToolkit.Maui,不再将其移除:

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

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).

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

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"/>

huangapple
  • 本文由 发表于 2023年6月12日 06:28:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定