Can't get custom UICollectionViewListCell to work in Xamarin iOS

WP (Smokeball) 146 Reputation points
2021-05-18T01:35:29.613+00:00

I've been trying to get my custom UICollectionViewListCell to work, I have a working Swift sample but when converted to Xamarin iOS in C# one of the most important calls don't get invoked, that is the MakeContentView method call.

Please see below for the implementation of my custom list cell. My understanding from experimenting with the Swift code is the moment you set the ContentConfigurration property, in your custom UIContentConfiguration class, the GetUpdatedConfiguration is called and the MakeContentView would be called. However when I debug only the GetUpdatedConfiguration is called but MakeContentView is never called, resulting in my list cell to be blank.

public class MenuItemCommandListCell : UICollectionViewListCell
        {
            protected internal MenuItemCommandListCell(IntPtr handle) : base(handle)
            {
            }

            public NSMenuItem DataContext { get; set; }

            public override void UpdateConfiguration(UICellConfigurationState state)
            {
                var backgroundConfiguration = UIBackgroundConfiguration.ListGroupedCellConfiguration;
                backgroundConfiguration.BackgroundColor = UIColor.SystemBackgroundColor;
                BackgroundConfiguration = backgroundConfiguration;
                BackgroundColor = UIColor.Clear;
                var newConfiguration = new MenuItemCommandContentConfiguration();
                newConfiguration.Name = (DataContext.Item as MainMenuSectionItem).DisplayName;
                AutomaticallyUpdatesContentConfiguration = true;

                ContentConfiguration = newConfiguration.GetUpdatedConfiguration(state);

            }
        }

        public class MenuItemCommandContentConfiguration : IUIContentConfiguration
        { 
            public IUIContentConfiguration GetUpdatedConfiguration(IUIConfigurationState state)
            {
                return this;
            }

            public IUIContentView MakeContentView()
            {
                return new MenuItemCommandView(this);
            }

            [return: Release]
            public NSObject Copy(NSZone zone)
            {
                throw new NotImplementedException("MenuItemCommandContentConfiguration Copy Method Not Implemented");
            }

            public void Dispose()
            {

            }

            public string Name { get; set; }
            public UIImage Icon { get; set; }
            public Action TargetAction { get; set; }

            public IntPtr Handle { get; }
        }

        public class MenuItemCommandView : UIView, IUIContentView
        {
            private UIButton _currentActionButton;

            public MenuItemCommandView(MenuItemCommandContentConfiguration configuration) : base(CGRect.Empty)
            {
                SetupAllViews();
                ApplyConfiguration(configuration);
            }

            private void SetupAllViews()
            {
                _currentActionButton = new UIButton(UIButtonType.System);
                _currentActionButton.Frame = new CGRect(0, 0, 50, 50);
                _currentActionButton.ContentMode = UIViewContentMode.Left;
                _currentActionButton.HorizontalAlignment = UIControlContentHorizontalAlignment.Left;
                _currentActionButton.BackgroundColor = UIColor.Clear;

                AddSubview(_currentActionButton);

                _currentActionButton.TranslatesAutoresizingMaskIntoConstraints = false;

                NSLayoutConstraint.ActivateConstraints(new[]
                {
                    _currentActionButton.LeadingAnchor.ConstraintEqualTo(LayoutMarginsGuide.LeadingAnchor),
                    _currentActionButton.TrailingAnchor.ConstraintEqualTo(LayoutMarginsGuide.TrailingAnchor),
                    _currentActionButton.TopAnchor.ConstraintEqualTo(LayoutMarginsGuide.TopAnchor),
                    _currentActionButton.BottomAnchor.ConstraintEqualTo(LayoutMarginsGuide.BottomAnchor)
                });
            }

            private void ApplyConfiguration(MenuItemCommandContentConfiguration configuration)
            {
                if (Configuration == configuration)
                {
                    return;
                }

                Configuration = configuration;

                _currentActionButton.SetTitle(configuration.Name, UIControlState.Normal);
                _currentActionButton.SetImage(configuration.Icon, UIControlState.Normal);
            }

            public IUIContentConfiguration Configuration { get; set; }
        }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,380 questions
0 comments No comments
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2021-05-18T07:58:13.307+00:00

    Hello,

    Welcome to Microsoft Q&A!

    • Make MenuItemCommandContentConfiguration inherit from NSObject , then the method Dispose and Handle is not needed to implement .
    • Add Export on MakeContentView method to make it expose in iOS .

    Complete code

       public class MenuItemCommandContentConfiguration : NSObject, IUIContentConfiguration  
           {    
               [return: Release]  
               public NSObject Copy(NSZone zone)  
               {  
                   return null;  
               }  
         
               public IUIContentConfiguration GetUpdatedConfiguration(IUIConfigurationState state)  
               {  
                   return this;  
               }  
         
               [Export("makeContentView")]  
               public IUIContentView MakeContentView()  
               {  
                   return  new MenuItemCommandView(this);  
               }  
         
               public string Name { get; set; }  
               public UIImage Icon { get; set; }  
               public Action TargetAction { get; set; }  
           }  
    

    Best Regards,
    Cole Xia


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.