Question on how best handle user control lifetimes
I'm porting an application from C# using Avalonia to C++ using Noesis.
In the Avalonia app, user controls are ephemeral. Once the user control is navigated away, it is disposed. This is because caching user controls in Avalonia can sometimes be buggy and cause memory leaks (they are working on it). However, ephemeral user controls create issues such as when I need to preserve where a user's current position is in a scroll view, its lost when the user navigates back to the area that calls for an instance of that view - unless I store the scroll offset in the view model.
I have created a DynamicContent control in Noesis which has a view model property. View models are explicitly linked to view file names. So in DynamicContent when the view model property changes, it loads the view for the given view model. See below
My question is, rather than load a new view every time, can I cache the view? I was thinking of adding the view ptr to the view model object. (It wouldn't be accessed by the view model itself, to preserve MVVM semantics).
Any flaws in my approach? Can user control instances be cached?
In the Avalonia app, user controls are ephemeral. Once the user control is navigated away, it is disposed. This is because caching user controls in Avalonia can sometimes be buggy and cause memory leaks (they are working on it). However, ephemeral user controls create issues such as when I need to preserve where a user's current position is in a scroll view, its lost when the user navigates back to the area that calls for an instance of that view - unless I store the scroll offset in the view model.
I have created a DynamicContent control in Noesis which has a view model property. View models are explicitly linked to view file names. So in DynamicContent when the view model property changes, it loads the view for the given view model. See below
Code: Select all
if (e.prop == ViewModelProperty)
{
auto viewModel = GetViewModel();
Ptr<FrameworkElement> view = Noesis::GUI::LoadXaml<FrameworkElement>
(viewModel->GetViewFileName());
if (!view)
{
NS_ERROR("Failed to load view. Make sure view is registered.");
return false;
}
view->SetDataContext(viewModel);
SetContent(view);
return true;
}Any flaws in my approach? Can user control instances be cached?
Re: Question on how best handle user control lifetimes
This is not necessary in Noesis. Our user controls keep the state when they are navigated away, hidden, collapsed, etc.In the Avalonia app, user controls are ephemeral. Once the user control is navigated away, it is disposed. This is because caching user controls in Avalonia can sometimes be buggy and cause memory leaks (they are working on it). However, ephemeral user controls create issues such as when I need to preserve where a user's current position is in a scroll view, its lost when the user navigates back to the area that calls for an instance of that view - unless I store the scroll offset in the view model.
Who is online
Users browsing this forum: Google [Bot] and 3 guests