Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a safe way to detach/unload behaviors without side effects?

In WPF and WinRT it is well known that behaviors do not call their Detach method reliably. The only way to avoid a memory leak is to subscribe to the Unloaded event of the AssociatedObject and unhook all events (example).

So far it works.

But I realized (as did someone else who commented on the blog entry I linked) that in this way unloaded behaviors are never attached again. Take a MenuFlyout for instance. If there is a behavior attached to a MenuFlyoutItem it is unloaded when the MenuFlyoutItem is closed. If you open the Menu again it won't be reattached.

This also happen with other UserControls. I "lose" behaviors when navigating in my WinRT App from one Page to another and back again although all Controls are recreated anew. I don't see how I can use them in a WinRT App where a lot of Controls are recreated by navigating around.

Is there any solution known to this?

like image 987
Amenti Avatar asked Jul 08 '14 18:07

Amenti


1 Answers

A big thanks to @Justin XL, Microsoft has updated the Behaviors SDK for UWP and made it open source. The new libraries fix the navigation detach problems! :)

http://blogs.windows.com/buildingapps/2015/11/30/xaml-behaviors-open-source-and-on-uwp/

https://twitter.com/hashtag/BehaviorsGoOpenSource?src=hash

like image 71
Maximus Avatar answered Nov 11 '22 17:11

Maximus