gpt4 book ai didi

event-handling - 我可以在C++/CX中使用lambda处理事件吗?

转载 作者:行者123 更新时间:2023-12-04 03:45:09 25 4
gpt4 key购买 nike

是否可以在C++/CX中使用lambda处理事件?例如,将这段代码从C#转换为C++/CX的最佳方法是什么?

this.animation.Completed += (s, e) =>
{
animation.Begin();
};

最佳答案

是的,这是正确的语法。但是,我们建议您使用函数处理程序代替lambda,因为lambda会引入循环引用并阻止释放内存。

http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh755799.aspx

In general, it's better to use a named function, rather than a lambda, for an event handler unless you take great care to avoid circular references. A named function captures the "this" pointer by weak reference, whereas a lambda captures it by strong reference and creates a circular reference. For more information, see Weak references and breaking cycles (C++/CX).

关于event-handling - 我可以在C++/CX中使用lambda处理事件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12479152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com