C2039 'XamlTypeInfo': 不是 XamlTypeInfo.g.cpp 中 'winrt::Microsoft::UI::Xaml' 的成员

C2039 'XamlTypeInfo': is not a member of 'winrt::Microsoft::UI::Xaml' in XamlTypeInfo.g.cpp

我将 Microsoft::UI::Xaml NuGet 包添加到我的 C++/WinRT 空白应用程序项目会引发错误。

输出消息:

1>------ Rebuild All started: Project: TxtRecordGenerator, Configuration: Debug x64 ------ 1>64 bit MIDLRT Processing C:\Users\a124p\Documents\GitHub\VisualizationRecorder\TxtRecordGenerator\App.idl 1>App.idl 1>64 bit MIDLRT Processing C:\Program Files (x86)\Windows Kits\Include.0.18362.0\winrt\winrtbase.idl 1>winrtbase.idl 1>64 bit MIDLRT Processing C:\Program Files (x86)\Windows Kits\Include.0.18362.0\winrt\midlbase.idl 1>midlbase.idl 1>Processing WinMD 1>Processing input metadata file x64\Debug\Unmerged\App.winmd. 1>Processing input metadata file x64\Debug\Unmerged\MainPage.winmd. 1>Processing input metadata file x64\Debug\Unmerged\XamlMetaDataProvider.winmd. 1>Saved output metadata file TxtRecordGenerator.winmd. 1>Validating metadata file x64\Debug\Merged\TxtRecordGenerator.winmd. 1>pch.cpp 1>App.cpp 1>MainPage.cpp 1>module.g.cpp 1>XamlTypeInfo.Impl.g.cpp 1>XamlTypeInfo.g.cpp 1>C:\Users\a124p\Documents\GitHub\VisualizationRecorder\TxtRecordGenerator\Generated Files\XamlTypeInfo.g.cpp(919,77): error C2039: 'XamlTypeInfo': is not a member of 'winrt::Microsoft::UI::Xaml' 1>C:\Users\a124p\Documents\GitHub\VisualizationRecorder\TxtRecordGenerator\Generated Files\winrt\Microsoft.UI.Xaml.Controls.h(16562): message : see declaration of 'winrt::Microsoft::UI::Xaml' 1>C:\Users\a124p\Documents\GitHub\VisualizationRecorder\TxtRecordGenerator\Generated Files\XamlTypeInfo.g.cpp(919,65): error C3083: 'XamlTypeInfo': the symbol to the left of a '::' must be a type 1>C:\Users\a124p\Documents\GitHub\VisualizationRecorder\TxtRecordGenerator\Generated Files\XamlTypeInfo.g.cpp(919,79): error C2039: 'XamlControlsXamlMetaDataProvider': is not a member of 'winrt::Microsoft::UI::Xaml' 1>C:\Users\a124p\Documents\GitHub\VisualizationRecorder\TxtRecordGenerator\Generated Files\winrt\Microsoft.UI.Xaml.Controls.h(16562): message : see declaration of 'winrt::Microsoft::UI::Xaml' 1>C:\Users\a124p\Documents\GitHub\VisualizationRecorder\TxtRecordGenerator\Generated Files\XamlTypeInfo.g.cpp(919,111): error C3861: 'XamlControlsXamlMetaDataProvider': identifier not found 1>Done building project "TxtRecordGenerator.vcxproj" -- FAILED. ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

这似乎是 WinRT C++ 代码生成器导致的错误。

C++/WinRT工程代码文件如下:

pch.h代码:

#pragma once
#include <windows.h>
#include <unknwn.h>
#include <restrictederrorinfo.h>
#include <hstring.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <winrt/Windows.UI.Xaml.h>
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.UI.Xaml.Controls.Primitives.h>
#include <winrt/Windows.UI.Xaml.Data.h>
#include <winrt/Windows.UI.Xaml.Interop.h>
#include <winrt/Windows.UI.Xaml.Markup.h>
#include <winrt/Windows.UI.Xaml.Navigation.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>

MainPage.idl:

namespace TxtRecordGenerator
{
    [default_interface]
    runtimeclass MainPage : Windows.UI.Xaml.Controls.Page
    {
        MainPage();
    }
}

MainPage.h:

#pragma once

#include "MainPage.g.h"

namespace winrt::TxtRecordGenerator::implementation
{
    struct MainPage : MainPageT<MainPage>
    {
        MainPage();
    };
}

namespace winrt::TxtRecordGenerator::factory_implementation
{
    struct MainPage : MainPageT<MainPage, implementation::MainPage>
    {
    };
}

MainPage.cpp:

#include "pch.h"
#include "MainPage.h"
#include "MainPage.g.cpp"

using namespace winrt;
using namespace Windows::UI::Xaml;

namespace winrt::TxtRecordGenerator::implementation
{
    MainPage::MainPage()
    {
        InitializeComponent();
    }
}

MainPage.xaml:

<Page
    x:Class="TxtRecordGenerator.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TxtRecordGenerator"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="using:Microsoft.UI.Xaml.Controls"
    mc:Ignorable="d">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <CalendarDatePicker x:Name="BeginDatePicker" 
                            PlaceholderText="Pick a date" 
                            Header="Begin Date" 
                            Foreground="{ThemeResource SystemAccentColorLight1}" 
                            Grid.Row="0" 
                            Grid.Column="0" 
                            HorizontalAlignment="Center" 
                            VerticalAlignment="Center" />
        <CalendarDatePicker x:Name="EndDatePicker" 
                            PlaceholderText="Pick a date" 
                            Header="End Date" 
                            Foreground="{ThemeResource SystemAccentColorLight1}" 
                            Grid.Row="0" 
                            Grid.Column="1" 
                            HorizontalAlignment="Center" 
                            VerticalAlignment="Center" />
        <controls:NumberBox x:Name="BeginNumberBox"
                   Header="Enter an integer:" 
                   Value="1" 
                   SpinButtonPlacementMode="Compact" 
                   SmallChange="10"
                   LargeChange="100"
                   Grid.Row="1"
                   Grid.Column="0"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"/>
        <controls:NumberBox x:Name="EndNumberBox"
                   Header="Enter an integer:" 
                   Value="1" 
                   SpinButtonPlacementMode="Compact" 
                   SmallChange="10"
                   LargeChange="100"
                   Grid.Row="1"
                   Grid.Column="1"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"/>
    </Grid>
</Page>

开发环境:

这个问题让我很困惑。谁能帮帮我?

首先需要在App.xaml中声明WinUI应用资源:

<Application ......>
    <Application.Resources>
        <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
    </Application.Resources>
</Application>

然后还要添加下面的头文件

#include "winrt/Microsoft.UI.Xaml.XamlTypeInfo.h"

更多的细节可以参考这篇文档:A simple C++/WinRT Windows UI Library example.