The view is not deleted after being removed from the region
Adding tabs to the application. I understand that my child views are not removed (finalized) after I remove them from Region
.
regionManager.Regions[regionName].Remove(tabItem.Content);
Each time I close the tab and open it again, a new instance is created, but the old instance remains open until I close the application. Test it with a Finalizer
Breakpoint. This causes my application to not release Region
and crash when it RegionManager
tries to create a scope that already exists.
Even
[RegionMemberLifetime(KeepAlive = false)]
will remove it from Region
, but the object is still alive.
source to share
I had a similar problem and just solved it this morning.
Before adding a new region using the regionManager, check to see if it exists:
this.regionManager.Regions.ContainsRegionWithName("Your region name")
Then, when I remove the view from the scope, I simply call the GC.Collect () method of the garbage collector to "Dispose" the removed views and free memory.
However, make sure you use the [RegionMemberLifetime (KeepAlive = false)] attribute in your view .
For more information see this post
Edit
Another solution using a one-off template, which I use for some views as well.
If my view implements the IDisposable interface, then the method looks like this:
public void Dispose()
{
GC.SuppressFinalize(this);
}
After that, when you remove the view from the scope, just call the Dispose method:
myRegion.Deactivate(view);
myRegion.Remove(view);
var disposable = view as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
Please note that I am using the mvvm template and I do not need to free any other managed object. Otherwise, if you have any managed objects within your view, see more details about IDisposable pattern here
source to share