Search code examples
c#xamarin.iosxamarin.formsdisposecustom-renderer

Xamarin iOS Cannot access a disposed object, Object name: ''


I am working with xamarin forms project and I have a bug on xamarin iOS project. Currently I implemented functionality of Master-Detail Page and also I added button item on action bar. Problem is when I am running application from my MainPage I am moving to another page from my burger menu and then back again to MainPage , everything seems good. But when I scroll my MainPage carousel to another object and I click on ActionBar item I am receiving such a error:

System.ObjectDisposedException: Cannot access a disposed object.

Object name: 'CustomNavigationRenderer'.

This is my iOS CustomNavigationRenderer class:

[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationRenderer))]
public class CustomNavigationRenderer : NavigationRenderer
{
    private string pName;
    private int pCCount;
    private string pCName;

    private App MainApp { get; set; }
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
    }
    private void CIndexChanged(int pCCount, string pName)
    {    
        UIBarButtonItem providerBtn = TopViewController.NavigationItem.RightBarButtonItems[0];
    }

On debug TopViewController I got such a line:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'CustomNavigationRenderer'.

But until I scroll from current object to another carousel view object TopViewController have such a line on debug mode:

Xamarin_Forms_Platform_iOS_NavigationRenderer_ParentingViewController

What could be main reason, that object do not reach that action bar item?


Solution

  • I solved this issue by do not creating new Navigation page all the time , but reusing existing Navigation page. Here is my Navigation items method where I am taking items from existing list. I reused Main page:

    private void ListView_ItemSelect(object sender, SelectedItemChangedEventArgs e)
            {
                var selectedItem = (MasterMenuItem)((ListView)sender).SelectedItem;
    
                MainPage mainPage = (App.Current.MainPage as MainPage);
                switch (selectedItem.KeyIndexName)
                {
                    case "MainPage":
                        mainPage.Detail = mainPage.MainPageDetail;
                        break;
                    case "AAA":
                        if(aaa==null)
                            aaa = new NavigationPage(new AaaPage());
                        mainPage.Detail = aaa;
                        break;
                    case "BBB":
                        if (bbb== null)
                            bbb = new NavigationPage(new BbbPage());
                        mainPage.Detail = bbb;
                        break;
                    case "CCC":
                        if (ccc == null)
                            ccc = new NavigationPage(new CccPage());
                        mainPage.Detail = ccc;
                        break;
                };
                mainPage.IsPresented = false;
            }