为WinUI3项目添加新窗口/对话框时出现编译错误
操作背景
我要创建一个可显示WinUI3对话框、供桌面应用调用的DLL,操作步骤如下:
- 通过「文件->新建->项目」创建Blank App Packaged WinUI3项目,编译后主窗口可正常显示,相关文件包含
mainwindow.xaml、mainwindow.idl、mainwindow.xaml.cpp、mainwindow.xaml.h。
第一次尝试添加ContentDialog的错误
尝试添加名为about.xaml的ContentDialog时,系统未自动生成对应的IDL文件,编译时报错:
fatal error C1083: Cannot open include file: 'About.g.h': No such file or directory
手动编辑原IDL文件,添加以下代码:
namespace wui3 { [default_interface] runtimeclass MainWindow : Microsoft.UI.Xaml.Window { MainWindow(); Int32 MyProperty; } runtimeclass About : Microsoft.UI.Xaml.Window { About(); Int32 MyProperty; } }
此时GeneratedFiles目录生成了about.g.h,但编译仍失败,且自动生成的.cpp文件为C#类型而非C++。
第二次尝试创建仅XAML的About窗口的错误
改用「Xaml View」创建仅包含XAML代码的About窗口,代码如下:
<Window x:Class="wui3.About" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:wui3" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <Button x:Name="myButton" Click="myButton_Click">Click Me</Button> </StackPanel> </Window>
编译时XamlTypeInfo.g.cpp出现报错,错误信息如下:
XamlTypeInfo.g.cpp
1>F:\TP2\wui3\Generated Files\XamlTypeInfo.g.cpp(144,55): error C2039: 'About': is not a member of 'winrt::wui3::implementation'
1>F:\TP2\wui3\Generated Files\XamlTypeInfo.g.cpp(20,11): message : see declaration of 'winrt::wui3::implementation'
1>F:\TP2\wui3\Generated Files\XamlTypeInfo.g.cpp(144,60): error C2275: 'winrt::wui3::About': expected an expression instead of a type
1>F:\TP2\wui3\Generated Files\XamlTypeInfo.g.cpp(144,6): error C2440: 'initializing': cannot convert from 'overloaded-function' to 'winrt::Windows::Foundation::IInspectable (__cdecl *)(void)'
1>F:\TP2\wui3\Generated Files\XamlTypeInfo.g.cpp(144,6): message : None of the functions with this name in scope match the target type
1>F:\TP2\wui3\Generated Files\XamlTypeInfo.g.cpp(150,5): error C2078: too many initializers
请问我哪里操作出错了?
内容的提问来源于stack exchange,提问作者Michael Chourdakis

