Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing lifecycle so Region created once fully parented to the Page
- Loading branch information
Showing
3 changed files
with
90 additions
and
6 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/Forms/Prism.Forms.Regions/Behaviors/RegionCleanupBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using Prism.Regions; | ||
using Xamarin.Forms; | ||
|
||
namespace Prism.Behaviors | ||
{ | ||
internal class RegionCleanupBehavior : BehaviorBase<Page> | ||
{ | ||
private WeakReference<IRegion> _regionReference; | ||
|
||
public RegionCleanupBehavior(IRegion region) | ||
{ | ||
_regionReference = new WeakReference<IRegion>(region); | ||
} | ||
|
||
public IRegion Region => _regionReference.TryGetTarget(out var target) ? target : null; | ||
|
||
protected override void OnDetachingFrom(Page bindable) | ||
{ | ||
if (Region != null && Region.RegionManager.Regions.ContainsRegionWithName(Region.Name)) | ||
{ | ||
Region.RegionManager.Regions.Remove(Region.Name); | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Forms/Prism.Forms.Regions/Extensions/ElementExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Xamarin.Forms; | ||
|
||
namespace Prism.Extensions | ||
{ | ||
internal static class ElementExtensions | ||
{ | ||
public static bool CheckForParentPage(this VisualElement visualElement) | ||
{ | ||
return GetParentPage(visualElement) != null; | ||
} | ||
|
||
public static Element GetRoot(this Element element) | ||
{ | ||
switch(element.Parent) | ||
{ | ||
case null: | ||
return element; | ||
default: | ||
return GetRoot(element.Parent); | ||
} | ||
} | ||
|
||
public static Page GetParentPage(this Element visualElement) | ||
{ | ||
switch(visualElement.Parent) | ||
{ | ||
case Page page: | ||
return page; | ||
case null: | ||
return null; | ||
default: | ||
return GetParentPage(visualElement.Parent); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters