2016-06-30 8 views
0

プリズムPubSubのスタイルのイベントの使用:ただし、直接変換私は次のようにC#での有効な構文は次のようになりますことを知って、C++/CLIでイベントアグリゲータを作成しようとしているC++/CLI

//C# code 
public partial class Foo : UserControl, IView, IDisposable 
{ 
    private IEventAggregator _aggregator; 

    public Foo(IEventAggregator aggregator) 
    { 
     InitializeComponent(); 

     this._aggregator = aggregator; 
     if (this._aggregator == null) 
      throw new Exception("null pointer"); 

     _subToken =_aggregator.GetEvent<fooEvent>().Subscribe(Handler, ThreadOption.UIThread, false); 
    } 

    private SubscriptionToken _subToken = null; 

    private void Handler(fooEventPayload args) 
    { 
     //this gets run on the event 
    } 
} 

これをC++/CLIに渡すと、指定された行の "管理されたクラスに対してメンバへのポインタが有効ではありません"というエラーが返されます。回避策はありますか?私はそれがC#が "アクション"を生成する方法と関係があると思います。

//C++/CLI code 
ref class Foo 
{ 
public: 
    Foo(IEventAggregator^ aggregator) 
    { 
     void InitializeComponent(); 

     this->_aggregator = aggregator; 
     if (this->_aggregator == nullptr) 
      throw gcnew Exception("null pointer"); 

     //error in the following line on Hander, a pointer-to-member is not valid for a managed class 
     _subToken = _aggregator->GetEvent<fooEvent^>()->Subscribe(Handler, ThreadOption::UIThread, false); 
private: 
    IEventAggregator^_aggregator; 
    SubscriptionToken^_addActorPipelineToken = nullptr; 

    void Handler(fooEventPayload^ args) 
    { 
     //this gets run on the event 
    } 
} 

答えて

1

デリゲートオブジェクトを明示的にインスタンス化する必要がありますが、C#でこれを行うことはできません。

_subToken = _aggregator->GetEvent<fooEvent^>()->Subscribe(
    gcnew Action<fooEventPayload^>(this, &Foo::Handler), ThreadOption::UIThread, false); 
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^      Explicitly instantiate the delegate. 
//         ^^^^    Object to call the delegate on. 
//          ^^^^^^^^^^^^^ C++-style reference to the method. 
関連する問題