Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
45
Why is IGGridView.Theme returning null after context switch?
posted

After updating our app to use Xamarin's Unified API and the recent SR release, I started getting object ref crashes with grid.Theme == null where previously it had been populated.  I tracked it down to the code below and was wondering if this is a defect or if you could explain the behavior.  I've got a workaround for the problem (setting the theme again inside the task) but I don't understand why the property is getting nulled to begin with.



        private IGGridView grid;


        private List<Section> CreateSections(CoreGraphics.CGRect bounds, UIView topView)
        {
            mainView = new UIView
            {
                Frame = new CoreGraphics.CGRect(0, 0, bounds.Width, bounds.Height),
                AutoresizingMask = UIViewAutoresizing.None,
                BackgroundColor = UIColor.Clear
            };

            grid = CreateGridView(gridFrame);
            grid.Theme = new PatientListGridTheme();  // custom Theme that inherits from IGGridViewThemeDefinition
            
            LoadGridData();        
            
            mainView.Add(grid);

            return new List<Section> { main };
        }

        
        private void LoadGridData()  // Invoked from UI thread
        {
            Console.WriteLine(string.Format("Theme : {0}", grid.Theme == null ? "NULL" : grid.Theme.GetType().ToString())); 

            //prints Theme : PatientListGridTheme

            Task.Run(async () =>
                {
                        await Task.Run(() =>
                        {
                            Console.WriteLine(string.Format("Theme : {0}", grid.Theme == null ? "NULL" : grid.Theme.GetType().ToString()));

                            //prints Theme : NULL !!!!!!!!


                            InvokeOnMainThread(() =>
                                {
                                    Console.WriteLine(string.Format("Theme : {0}", grid.Theme == null ? "NULL" : grid.Theme.GetType().ToString()));

                                    //prints Theme : NULL

                                    grid.Theme = new PatientListGridTheme();

                                    Console.WriteLine(string.Format("Theme : {0}", grid.Theme == null ? "NULL" : grid.Theme.GetType().ToString()));

                                    //prints Theme : PatientListGridTheme

                                });
                        }
                        );

                        InvokeOnMainThread(() => HideLoadingView());
                });
        }