-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Replace Page with correct type when reloaded
Create an instance of the same type as the element being reloaded, and replace the old element in the visual tree rather than nesting inside of it.
- Loading branch information
1 parent
c26bd89
commit 64a25da
Showing
4 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
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
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,41 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Android.Views; | ||
using Uno.Extensions; | ||
using Uno.UI; | ||
|
||
namespace Windows.UI.Xaml.Media | ||
{ | ||
public partial class VisualTreeHelper | ||
{ | ||
internal static void SwapViews(UIElement oldView, UIElement newView) | ||
{ | ||
var parentViewGroup = oldView?.Parent as ViewGroup; | ||
var currentPosition = parentViewGroup?.GetChildren().IndexOf(oldView); | ||
|
||
if (parentViewGroup != null && currentPosition != null && currentPosition.Value != -1) | ||
{ | ||
parentViewGroup.RemoveViewAt(currentPosition.Value); | ||
|
||
var unoViewGroup = parentViewGroup as UnoViewGroup; | ||
|
||
if (unoViewGroup != null) | ||
{ | ||
var newContentAsFrameworkElement = newView as IFrameworkElement; | ||
if (newContentAsFrameworkElement != null) | ||
{ | ||
newContentAsFrameworkElement.TemplatedParent = (unoViewGroup as IFrameworkElement)?.TemplatedParent; | ||
} | ||
unoViewGroup.AddView(newView, currentPosition.Value); | ||
} | ||
else | ||
{ | ||
parentViewGroup.AddView(newView, currentPosition.Value); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,28 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Uno.Extensions; | ||
|
||
namespace Windows.UI.Xaml.Media | ||
{ | ||
public partial class VisualTreeHelper | ||
{ | ||
internal static void SwapViews(UIElement oldView, UIElement newView) | ||
{ | ||
var currentPosition = oldView?.Superview?.Subviews.IndexOf(oldView) ?? -1; | ||
|
||
if (currentPosition != -1) { | ||
var currentSuperview = oldView?.Superview; | ||
oldView?.RemoveFromSuperview(); | ||
|
||
#if __IOS__ | ||
currentSuperview?.InsertSubview(newView, currentPosition); | ||
#elif __MACOS__ | ||
currentSuperview.AddSubview(newView, NSWindowOrderingMode.Above, currentSuperview.Subviews[Math.Max(0, currentPosition-1)]); | ||
#endif | ||
} | ||
} | ||
} | ||
} |
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,27 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Uno.Extensions; | ||
|
||
namespace Windows.UI.Xaml.Media | ||
{ | ||
public partial class VisualTreeHelper | ||
{ | ||
internal static void SwapViews(UIElement oldView, UIElement newView) | ||
{ | ||
if ((oldView as FrameworkElement)?.Parent is FrameworkElement parentElement) | ||
{ | ||
var currentPosition = parentElement.GetChildren().IndexOf(oldView); | ||
|
||
if (currentPosition != -1) | ||
{ | ||
parentElement.RemoveChild(oldView); | ||
|
||
parentElement.AddChild(newView, currentPosition); | ||
} | ||
} | ||
} | ||
} | ||
} |