【发布时间】:2021-04-05 15:51:27
【问题描述】:
我是 WinRT 新手。我正在将编写为 C++/CX 的 Windows UWP 应用程序转换为 C++/WinRT。我有一个 C++/CX ref 类,它的作用与 C# 中的 Microsoft.VisualStudio.PlatformUI.DelegateCommand 类基本相同。我的 C++ 类实现了ICommand,其中Execute 和CanExecute 回调由委托处理。头文件的缩写代码如下所示:
public delegate void ExecuteDelegate(Platform::Object^ parameter);
public delegate bool CanExecuteDelegate(Platform::Object^ parameter);
public ref class DelegateCommand sealed :
Windows::UI::Xaml::DependencyObject,
Windows::UI::Xaml::Data::INotifyPropertyChanged,
public Windows::UI::Xaml::Input::ICommand
{
public:
DelegateCommand(ExecuteDelegate^ execute, CanExecuteDelegate^ canExecute);
.
.
.
private:
ExecuteDelegate^ m_executeDelegate = nullptr;
CanExecuteDelegate^ m_canExecuteDelegate = nullptr;
.
.
.
};
我的DelegateCommand 类的典型实例化将弱指针和函数指针从具有Execute 和CanExecute 方法实现的类传递到构造函数:
Commands::Instance->UndoCommand = ref new DelegateCommand(
ref new ExecuteDelegate(this, &SVGDocumentUserControl::ExecuteUndoCommand),
ref new CanExecuteDelegate(this, &SVGDocumentUserControl::CanExecuteUndoCommand));
我确信这是一个简单的问题,但从我所读到的内容中我并不清楚,那么究竟如何在 C++/WinRT 中定义两个 C++/CX 委托?
public delegate void ExecuteDelegate(Platform::Object^ parameter);
public delegate bool CanExecuteDelegate(Platform::Object^ parameter);
[编辑:我还在摸索如何在 *.idl 文件中定义构造函数的函数指针。]
感谢您的耐心等待。
【问题讨论】:
-
只是一个想法,当您将现有代码提供给 C++/CX 编译器时,它不能以让您看到的方式导出元数据(我猜那将是
winmd格式)正确的原始声明? -
@BenVoigt:我在我的 CX 应用程序的 *.winmd 文件上运行了 winmdidl.exe。我需要的位,特别是委托定义,不在那里。好主意。
-
据我了解,您的 C++/CX 代码确实会生成一个 .winmd 文件。如果是这样,您可以改为针对该 .winmd 运行 C++/WinRT 的 cppwinrt.exe,并让它生成相应的头文件。这应该会给你完整的声明,包括代表。
-
还有Move to C++/WinRT from C++/CX 有一个关于代表的部分,所以也许这也有帮助。
-
@IInspectable 我阅读了关于代表的那部分。我希望我弄错了,但它似乎没有涵盖我如何尝试使用它们。我刚刚在 *.winmd 文件上尝试了 cppwinrt.exe。我收到错误消息:“找不到类型‘Windows.UI.Xaml.FlowDirection’。”是否有我应该添加到开发人员控制台命令行的包含/库目录?谢谢。