Replies: 3 comments 3 replies
-
I still haven't found a good solution to this problem. What is the recommended way of releasing resources managed by a ViewModel when it is no longer in use? |
Beta Was this translation helpful? Give feedback.
-
@Nurgo the viewmodel lifecycle should be paired with the corresponding view, so when the view is cleaned up, the viewmodel should be as well. Which region type (eg Frame, Tab, NavigationView) are you using? and would you be able to provide a sample project that demonstrates the behavior you're seeing? |
Beta Was this translation helpful? Give feedback.
-
Hi @nickrandolph, thank you for your prompt response. In my case, the view is a ContentDialog. Here is how the View, ViewModel and data object are registered: private static void RegisterRoutes(IViewRegistry views, IRouteRegistry routes) {
views.Register(
new DataViewMap<MyDialog, MyViewModel, MyData>(),
// ...
);
routes.Register(
// ...
);
} And here is how the dialog is opened (from a ViewModel): var response = await _navigator.NavigateViewModelForResultAsync<MyViewModel, bool>(this, data: myData, qualifier: Qualifiers.Dialog).AsResult(); MyViewModel implements IDisposable, but the Dispose method is never called. However, a new View and a new ViewModel are created each time the dialog is opened. |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm using Uno Platform with Uno Extensions for ViewModel navigation. I've noticed that when navigating away, the previous ViewModel isn't automatically disposed, potentially leaving resources unmanaged.
What's the recommended approach for releasing resources in a ViewModel created by the Navigator? Should we manually implement and call IDisposable, or is there another suggested method?
Additionally, is it possible to make the ViewModel navigation-aware, so it can respond to navigation events (e.g., being navigated away from), which could be used for resource cleanup?
Beta Was this translation helpful? Give feedback.
All reactions