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; }
}