【问题标题】:How to add a click handler to a WPF Button in C++/CLR?如何在 C++/CLR 中向 WPF 按钮添加点击处理程序?
【发布时间】:2010-12-13 10:59:17
【问题描述】:

在 C# 中,我可以做 (*):

        Button b = new Button();
        b.Click += ButtonOnClick;
    :
    void ButtonOnClick(object sender, RoutedEventArgs e)
    {
        // do something
    }

但在 C++/CLI 中我做不到:

    Button ^ b = gcnew Button();
    b->Click += ButtonOnClick;
:
void ButtonOnClick(Object ^ sender, RoutedEventArgs ^ e)
{
    // do something
}

我收到一个编译器错误,抱怨 += ButtonOnClick: 2>.\blub.cpp(108) : 错误 C3867: 'MyListBoxItem::ButtonOnClick': 函数调用缺少参数列表;使用 '&MyListBoxItem::ButtonOnClick' 创建指向成员的指针

(编译器给我的提示不起作用,因为它不是静态方法。)

C++/CLI 中 (*) 的等价物是什么?

谢谢 马克

【问题讨论】:

    标签: wpf button c++-cli click handler


    【解决方案1】:

    试试 罢工>

     b->Click += MAKE_DELEGATE( System::EventHandler, ButtonOnClick );
    

    b->Click += gcnew System::EventHandler(this, &ButtonOnClick);
    

    【讨论】:

    • 这给了我:错误 C2275:'System::EventHandler':非法使用这种类型作为表达式。在 msdn 中查找 MAKE_DELEGATE 会将我带到 MFC 库参考。但我想,我必须以某种方式创建一个(.net)代表 - 但我不知道如何。
    • 哦,虽然这也行不通,但它为我指明了正确的方向。这是有效的: b->Click += gcnew RoutedEventHandler(this, &ClassNameWhereTheButtonOnClickMethodIsIn::ButtonOnClick);谢谢 :) - 我现在应该将什么标记为答案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2015-12-05
    相关资源
    最近更新 更多