问题描述
我是 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 文件中定义构造函数的函数指针。]
感谢您的耐心等待。
解决方法
我看到您的代码将委托作为公共类型(非内部)。因此,假设您确实需要它们成为组件公共 API 的一部分,您只需在 MIDL3 中定义您的委托(以及一个带有构造函数的测试类,如下所示:
namespace BlankApp1
{
delegate void ExecuteDelegate(Object param);
delegate Boolean CanExecuteDelegate(Object param);
[default_interface]
runtimeclass TestClass
{
TestClass(ExecuteDelegate execute,CanExecuteDelegate canExecute);
}
}
从那里,您为这些委托投影了类型,以及 C++/WinRT 为委托提供的所有丰富支持。例如,TestClass
的实现很简单:
struct TestClass : TestClassT<TestClass>
{
TestClass() = default;
TestClass(BlankApp1::ExecuteDelegate const& execute,BlankApp1::CanExecuteDelegate const& canExecute)
: m_execute(execute),m_canExecute(canExecute)
{}
private:
ExecuteDelegate m_execute;
CanExecuteDelegate m_canExecute;
};
并且实例化该类型,使用 C++/WinRT 丰富的委托支持很容易,并且将为您提供与 C++/CX 相同的生命周期安全语义(即,存储对象的弱引用及其成员函数指针).
struct MyClass : winrt::implements<MyClass,winrt::Windows::Foundation::IInspectable>
{
MyClass() = default;
void Execute(winrt::Windows::Foundation::IInspectable const&)
{
}
bool CanExecute(winrt::Windows::Foundation::IInspectable const&)
{
return true;
}
TestClass CreateCommand()
{
return TestClass{
ExecuteDelegate{ get_weak(),&MyClass::Execute },CanExecuteDelegate{ get_strong(),&MyClass::CanExecute }
};
}
};