【问题标题】:Converting the C++/CX "delegate" object over to its equivalent in C++/WinRT将 C++/CX“委托”对象转换为 C++/WinRT 中的等效对象
【发布时间】:2021-04-05 15:51:27
【问题描述】:

我是 WinRT 新手。我正在将编写为 C++/CX 的 Windows UWP 应用程序转换为 C++/WinRT。我有一个 C++/CX ref 类,它的作用与 C# 中的 Microsoft.VisualStudio.PlatformUI.DelegateCommand 类基本相同。我的 C++ 类实现了ICommand,其中ExecuteCanExecute 回调由委托处理。头文件的缩写代码如下所示:

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 类的典型实例化将弱指针和函数指针从具有ExecuteCanExecute 方法实现的类传递到构造函数:

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’。”是否有我应该添加到开发人员控制台命令行的包含/库目录?谢谢。

标签: c++-cx c++-winrt


【解决方案1】:

我看到您的代码将委托作为公共类型(不是内部)。因此,假设您确实需要它们成为组件的公共 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 }
        };
    }
};

【讨论】:

  • 成功了。 *.idl 文件中代表的正确声明让我失望。而且它们在我直接使用的源代码中不可见。谢谢你。我现在在做生意!
猜你喜欢
  • 2020-11-27
  • 1970-01-01
  • 2017-08-16
  • 2016-04-24
  • 1970-01-01
  • 2012-11-22
  • 2021-03-10
  • 2011-01-23
  • 1970-01-01
相关资源
最近更新 更多