Created
July 7, 2020 22:23
-
-
Save pictos/9009669761ceaf7096b38fd71a96e69e to your computer and use it in GitHub Desktop.
Work around to use remove page with XF
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Foundation; | |
using RemovePageReproduction.iOS; | |
using UIKit; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Internals; | |
using Xamarin.Forms.Platform.iOS; | |
[assembly:ExportRenderer(typeof(Shell), typeof(MyShell))] | |
namespace RemovePageReproduction.iOS | |
{ | |
public class MyShell : ShellRenderer | |
{ | |
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection) | |
{ | |
//return base.CreateShellSectionRenderer(shellSection); | |
return new MyShellSectionR(this); | |
} | |
} | |
class MyShellSectionR : ShellSectionRenderer | |
{ | |
Dictionary<Element, IShellPageRendererTracker> _trackers; | |
public MyShellSectionR(IShellContext context) : base(context) | |
{ | |
var prop = GetType().BaseType.GetField(nameof(_trackers), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | |
var value = prop.GetValue(this); | |
_trackers = (Dictionary<Element, IShellPageRendererTracker>)value; | |
} | |
protected override void OnRemoveRequested(NavigationRequestedEventArgs e) | |
{ | |
var page = e.Page; | |
var renderer = Platform.GetRenderer(page); | |
var viewController = renderer?.ViewController; | |
if (viewController == null && _trackers.ContainsKey(page)) | |
viewController = _trackers[page].ViewController; | |
if (viewController != null) | |
{ | |
ViewControllers = ViewControllers.Remove(viewController); | |
DisposePage(page); | |
} | |
} | |
void DisposePage(Page page) | |
{ | |
var method = this.GetType().BaseType.GetMethod("DisposePage", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | |
method.Invoke(this, new object[] { page, false }); | |
} | |
} | |
static class ArrayEx | |
{ | |
public static T[] Remove<T>(this T[] self, T item) | |
{ | |
return self.RemoveAt(self.IndexOf(item)); | |
} | |
public static T[] RemoveAt<T>(this T[] self, int index) | |
{ | |
var result = new T[self.Length - 1]; | |
if (index > 0) | |
Array.Copy(self, result, index); | |
if (index < self.Length - 1) | |
Array.Copy(self, index + 1, result, index, self.Length - index - 1); | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment