2010-09-19 40 views
2

在C#中从click事件的委托,我们可以调用从按钮单击一个新的功能,像这样的论点,调用使用C++/CLI

////My function 
    public static void Method1(int x) 
    { 
     Console.WriteLine("Method 1"); 
    } 

,并设置此功能在这样一个命令按钮的单击事件,

button1.Click += delegate { mydelegate with argument }; 

如:

delegate void Procedure(int x); 

public partial class Window1 : Window 
{ 
    public Window1() 
    { 
     Procedure pProcedure = new Procedure(Method1); 
     InitializeComponent(); 

     button1.Click += delegate { pProcedure(10); }; 
    } 

    public static void Method1(int x) 
    { 
     Console.WriteLine("Method 1"); 
    } 
} 

现在,当我们点击Button1的,则函数 “方法一” 将invok即

如何使用C++/CLI执行相同操作?

我需要从click事件中找到添加的委托并需要删除。我怎样才能做到这一点?

回答

3

如果您在C++/CLI中询问如何使用匿名委托人,那么答案是不可以。在C++/CLI中,委托必须绑定到一个命名函数。

要完成内联匿名代表在C#中实际执行的操作,可以使用'functor'或的概念。以下C++/CLI示例说明如何创建一个函数对象,并将其“绑定”到一个特定的值,然后演示如何将其用作事件订阅者。

using namespace System; 

// Sample class with one event 'Started' 
public ref class Widget 
{ 
public: 
    Widget() 
    { 
    } 

    event EventHandler^Started; 

    void Start() 
    { 
     Console::WriteLine("Starting..."); 
     Started(this, EventArgs::Empty); 
    } 
}; 

// Declare 'functor' class to capture state 
private ref class Functor 
{ 
public: 
    Functor(int input) 
     : input_(input) 
    { 
    } 

    // This is what we will use as the handler method 
    void Handler(Object^sender, EventArgs^e) 
    { 
     Console::WriteLine(L"Invoked with input {0}.", input_); 
    } 

private: 
    int input_; 
}; 

// Entry point 
int wmain(int argc, wchar_t ** argv) 
{ 
    // Create a functor to capture value '10' 
    Functor^f = gcnew Functor(10); 

    Widget^widget = gcnew Widget(); 

    // Subscribe to event using functor's handler 
    // (note that we bind to the instance 'f' here) 
    EventHandler^handler = gcnew EventHandler(f, &Functor::Handler); 
    widget->Started += handler; 

    // Should print "Invoked with input 10." 
    widget->Start(); 

    // Remove the handler 
    widget->Started -= handler; 

    // Should not print anything extra now 
    widget->Start(); 

    return 0; 
} 
+0

谢谢您的回答。 – sabeesh 2010-09-19 05:40:23

+1

你不能*继续*。 C++ 0x添加lambdas,为C++首次提供了匿名方法的语法。就个人而言,我很高兴微软等待标准委员会就语法达成一致意见,而不是添加一些不可移植的扩展,并以受管vs本地代码中的lambda表达式的不同语法结束。 – 2010-09-20 17:08:58

+0

我需要从click事件中找到添加的委托并需要删除。我怎样才能做到这一点? – sabeesh 2010-09-22 12:01:27

1

谢谢你的帮忙。 在你的帮助下,我可以解决我的问题。 该解决方案是这样的,

//FirstWindow.h

#pragma once 

using namespace System; 
using namespace System::Windows; 
using namespace System::Windows::Controls; 

ref class Functor; 

ref class FirstWindow : Window 
{ 
    Canvas^ maincanvas; 
    Button^ addbutton1; 
    Button^ addbutton2; 
    Functor^ pFunctor; 
public: 
    FirstWindow(void); 
    void InitControls(void); 
    void MyFunction(int x, int y); 
}; 

//FirstWindow.cpp

#include "FirstWindow.h" 
#include "Functor.h" 

FirstWindow::FirstWindow(void) 
{ 
    Title = "First Avalon App"; 
    Width = 400; 
    Height = 400; 
    ResizeMode = System::Windows::ResizeMode::NoResize; 

    InitControls(); 
} 

void FirstWindow::InitControls(void) 
{ 
     addbutton1 = gcnew Button(); 
     addbutton1->Width = 80; 
     addbutton1->Height = 25; 
     addbutton1->Content = "Add"; 
     pFunctor = gcnew Functor(this, 10, 20); 
     addbutton1->Click += gcnew RoutedEventHandler(pFunctor, &Functor::Handler); 

     Canvas::SetTop(addbutton1, 45); 
     Canvas::SetLeft(addbutton1, 200); 

     pFunctor = gcnew Functor(this, 100, 200); 
     addbutton2 = gcnew Button(); 
     addbutton2->Width = 80; 
     addbutton2->Height = 25; 
     addbutton2->Content = "Add"; 
     addbutton2->Click += gcnew RoutedEventHandler(pFunctor, &Functor::Handler); 

     Canvas::SetTop(addbutton2, 85); 
     Canvas::SetLeft(addbutton2, 200); 

     maincanvas = gcnew Canvas();   

     maincanvas->Children->Add(addbutton1); 
     maincanvas->Children->Add(addbutton2); 
     Content = maincanvas; 
} 

void FirstWindow::MyFunction(int x, int y) 
{ 
    MessageBox::Show("This function is call by Button Click with values " + x.ToString() + " , " + y.ToString()); 
} 

//Functor.h

#pragma once 

using namespace System; 
using namespace System::Windows; 
using namespace System::Windows::Controls; 

ref class FirstWindow; 

private ref class Functor 
{ 
public: 
    Functor(FirstWindow^ pFirstWindow, int pFirstArg, int pSecArg); 

    // This is what we will use as the handler method 
    void Handler(Object^sender, RoutedEventArgs^e); 

private: 
    int m_pFirstArg; 
    int m_pSecArg; 
    FirstWindow^ m_pFirstWindow; 
}; 

//函子.cpp

#include "Functor.h" 
#include "FirstWindow.h" 

Functor::Functor(FirstWindow^ pFirstWindow, int pFirstArg, int pSecArg) : m_pFirstWindow(pFirstWindow), m_pFirstArg(pFirstArg), m_pSecArg(pSecArg) 
{ 

} 

void Functor::Handler(Object^sender, RoutedEventArgs^e) 
{ 
    if (m_pFirstWindow) 
     m_pFirstWindow->MyFunction(m_pFirstArg, m_pSecArg); 

} 

现在,当我们点击按钮1时,应用程序调用值为10,20的函数“MyFunction”,当我们点击按钮2时,同样的函数“MyFunction”的值为100,200。

谢谢你的帮助。

Sabeesh

+1

请编辑你的问题,而不是在这里添加答案。你应该删除它。 请参阅我的最新回复以了解您后续问题的答案。 – bobbymcr 2010-09-19 06:15:58