Search code examples
c#serializationdeserializationresx

UserControl invalid .resx file could not load type - Design time support needed


What I'm trying to do:

I'm creating a custom control that will work something along the lines of a ListView (Groups and items).

The control has groups and groups have items, so one could first create one group, then add one or more items to that group (under that groups properties).

The Issue:

I'm running into this error when I try to build my project:

Invalid Resx file.

Could not load type Widget.ListCategory, Widget, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null which is used in the .RESX file.

Ensure that the necessary references have been added to your project. Line 186, position 5. Widget C:\Users\mb.ITHOZTING\Dropbox\Widget\Widget\Form1.resx 186

Attempts to fix the project:

I have tried quite a lot of different things, so you must excuse me, I can't remember all of them, but these are those I remember:

  • I have tried implementing the ISerializer interface, didn't really do anything to my recollection.

  • I have tried deleting the generated code in the .Resx file, and then reconfiguring the control. Didn't help.

  • I have tried marking different properties and classes with different tags, e.g [Serializable].

Code

This is the culprit from the error message:

[Serializable]
class ListCategory : ListItemBase
{
    #region Properties
    private List<ListItem> items = new List<ListItem>();
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor(typeof(ListItemCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public List<ListItem> Items
    {
        get { return items; }
        set { items = value; }
    }
    #endregion
}

Now there's quite a few files and a good amount of code, so here's the whole project: https://www.dropbox.com/sh/zwz72dqm7vii78v/AACLjpy2DYZyQPxP_BBf0WTQa?dl=0

What steps are I'm supposed to take to fix this?


Solution

  • I have possibly found the solution, but I'm not 100% sure, as I feel it's been a bit unstable (not the solution, but the issue), so I won't mark this as the answer yet.

    What I had to do though, was to add a TypeConverter for each class that was giving me issues.

    E.g the first one was the ListCategory, then the other classes started acting up, and then I just had to follow the same procedure till it worked.

    I mixed the TypeConverter together from two different articles on providing rich design-time support.

    These are the links:

    1. http://www.codeproject.com/Articles/9667/Creating-Custom-Controls-Providing-Design-Time-Sup
    2. http://www.codeproject.com/Articles/5502/Creating-Collection-Based-Controls-with-Rich-Desig

    This is the resulting TypeConverter:

    class GroupListConverter : TypeConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(typeof(GroupList));
        }
    
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
        {
            if (destType == typeof(InstanceDescriptor))
                return true;
    
            return base.CanConvertTo(context, destType);
        }
    
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
        {
            if (destType == typeof(InstanceDescriptor))
            {
                System.Reflection.ConstructorInfo ci = typeof(GroupList).GetConstructor(System.Type.EmptyTypes);
    
                return new InstanceDescriptor(ci, null, false);
            }
    
            return base.ConvertTo(context, culture, value, destType);
        }
    }