Is it possible to handle an event with a lambda in C++/CX? As an example, what would be the best way to convert this snippet of code from C# into C++/CX?
this.animation.Completed += (s, e) =>
{
animation.Begin();
};
Is it possible to handle an event with a lambda in C++/CX? As an example, what would be the best way to convert this snippet of code from C# into C++/CX?
this.animation.Completed += (s, e) =>
{
animation.Begin();
};
Yes, that's the correct syntax. However, we recommend that you use a function handler instead of a lambda because a lambda can introduce a circular references and prevent memory from being freed.
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).
Here's what I ended up doing.
animation->Completed += ref new EventHandler<Object^>([this](Object^, Object^)
{
animtion->Begin();
});
© 2022 - 2024 — McMap. All rights reserved.