从 c++ 订阅 c# CompositeCommand

发布于 2024-10-08 15:23:25 字数 757 浏览 1 评论 0原文

在 ac# 程序集中,我有一个全局棱镜 CompositeCommand 可以订阅,如下所示:

private static readonly CompositeCommand __myCommand = new CompositeCommand();

public static CompositeCommand MyCommand
{
  get { return _myCommand; }
}

从 c# 我可以使用以下方式订阅此命令:

[...].MyCommand.RegisterCommand(new DelegateCommand<MyClass>((c) => c.Something()));

我的问题:我需要从托管 c++ 订阅该命令,我不知道该函数如何签名需要在 DelegateCommand 中使用。大多数时候我会收到如下错误:

error C2664: 'Microsoft::Practices::Prism::Commands::DelegateCommand::DelegateCommand(System::Action ^)': conversion ofparameter 1 from 'void (__clrcall *)( “System::Action ^”中的“MyNameSpace::MyClass ^)”不可能。

如何订阅 ac# 命令?或者是否有其他方法来监听事件(我可以用不同的东西替换 CompositeCommand)。

谢谢!

In a c# assembly I've got a global prism CompositeCommand to subscribe to like this:

private static readonly CompositeCommand __myCommand = new CompositeCommand();

public static CompositeCommand MyCommand
{
  get { return _myCommand; }
}

From c# I can subscribe to this command using:

[...].MyCommand.RegisterCommand(new DelegateCommand<MyClass>((c) => c.Something()));

My problem: I need to subscribe to the command from managed c++, and I got no idea how the function signature needs to be to be used in DelegateCommand. Most of the time I get errors like:

error C2664: 'Microsoft::Practices::Prism::Commands::DelegateCommand::DelegateCommand(System::Action ^)': conversion of parameter 1 from 'void (__clrcall *)(MyNameSpace::MyClass ^)' in 'System::Action ^' not possible.

How do I subscribe to a c# command? Or is there some other way to listen to an event (I can replace the CompositeCommand with something different).

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2024-10-15 15:23:25

我假设您正在使用托管 C++ - 其他任何东西,并且还有更大的问题需要担心。

您似乎遇到了与 C# 类型的链接错误。因此,我认为该问题与 Prism 的任何问题无关。为了使 C++ 托管编译器链接到 C# 程序集,您需要生成带有 XML 文档文件的 C# 程序集(请参阅属性中的“生成”选项卡)。您的项目中启用了该功能吗?

我使用以下内容作为一个非常简单的概念证明,其中 TestObject 是在 C++ DLL 引用的 C# 程序集中定义的。这编译没有任何问题。

头文件:

 void __clrcall CommandCallback(Project::Infrastructure::TestObject^ param);

public ref class ManagedModule : IModule
{
public:
    ManagedModule();

     virtual void __clrcall Initialize();

private:
};

实现:

ManagedDLL::ManagedModule::ManagedModule()
{

}

void __clrcall ManagedDLL::ManagedModule::Initialize()
{
    Action<Project::Infrastructure::TestObject^>^ newAction = 
         gcnew Action<Project::Infrastructure::TestObject^>(&CommandCallback);
    DelegateCommand<Project::Infrastructure::TestObject^>^ newCommand = 
         gcnew DelegateCommand<Project::Infrastructure::TestObject^>(newAction);
    Project::Infrastructure::Commands::ApplicationExitCommand->RegisterCommand(newCommand);

    return;
}

void __clrcall CommandCallback(Project::Infrastructure::TestObject^ param)
{
    return;
}

I'm going to assume that you're using managed C++ - anything else, and there's bigger issues to worry about.

It looks like you're getting linking errors to your C# type. As such, I don't think the issue is related to any problems with Prism. In order for the C++ managed compiler to link to your C# assembly, you need to produce the C# assembly with an XML documentation file (see the Build tab in your properties). Is that enabled in your project?

I used the following as a very simple proof of concept, where TestObject is defined in the C# assembly referenced by the C++ DLL. This compiled without any issues.

Header file:

 void __clrcall CommandCallback(Project::Infrastructure::TestObject^ param);

public ref class ManagedModule : IModule
{
public:
    ManagedModule();

     virtual void __clrcall Initialize();

private:
};

Implementation:

ManagedDLL::ManagedModule::ManagedModule()
{

}

void __clrcall ManagedDLL::ManagedModule::Initialize()
{
    Action<Project::Infrastructure::TestObject^>^ newAction = 
         gcnew Action<Project::Infrastructure::TestObject^>(&CommandCallback);
    DelegateCommand<Project::Infrastructure::TestObject^>^ newCommand = 
         gcnew DelegateCommand<Project::Infrastructure::TestObject^>(newAction);
    Project::Infrastructure::Commands::ApplicationExitCommand->RegisterCommand(newCommand);

    return;
}

void __clrcall CommandCallback(Project::Infrastructure::TestObject^ param)
{
    return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文