Editing RESX files in Visual Studio is not very convenient if you want to add content like image streams, icons and things like that. I work a lot with resource files because I don't like to add a form to a library just to be able to add an image stream as a resource.
I know that there are already pretty good open source RESX editors. But they have one thing in common that I don't like: If I want to add support for a new resource type, I have to change the code of the library. But what I want is a library that supports latebinding, so that I can provide my userdefined libraries with definitions of different resources.
The TicoResourceWriter library basically contains the following classes:
ResourceManager is a class that allows you to edit the Resource File. It's derived from System.Collections.CollectionBase. The Item type is IResourceHolder. ClassLoader is a class that manages the several combinations of IResourceHolder and appropriate IResourceEditors. It's also derived from System.Collections.CollectionBase. It's self-managed so you should not try to add/remove items :) the Item type is ClassLoaderItem. ClassLoaderItem is a class that manages one ResourceType. It allows you to create the IResourceHolder instance for the Resource you want to add, and the IResourceEditor instance to edit your Resource. IResourceHolder is the Interface for any class that holds a Resource. It provides the Type of the Resource, the Name and the Resource itself. IResourceEditor is the Interface for any class that provides editing of Resources. DefaultResourceHolder is an abstract basic-Implementation of a Resource holder. DefaultResourceEditor is an abstract basic-Implementation of a Resource editor. ResourcePropertiesAttribute is an attribute that allows you to tell the ClassLoader which Resource kind you want to manage with which IResourceHolder, and what editor class you want to use. StringHolder/Editor, IconHolder/Editor, ImageHolder/Editor and ImageStreamHolder/Editor. To open a Resource file, just do the following:
ResourceManager mgr = new ResourceManager("C:\\blah.resx");
IResourceHolder itm = mgr.CreateHolderInstance(0);
mgr.Add(itm);
mgr.Edit(itm);
mgr.Save()
This creates an RESX file on c:\Blah.resx and adds one resource Item on it.
If you open a Resource File containing a Resource that has no appropriate IResourceHolder/Editor, ResourceManager will not let you save it. That's because the unknown Resources would be lost if you would save it.
So, if you want to have a complete Resource editor, you have to provide some classes by yourself. Just create a new ClassLibrary project and add TicoResourceWriter as reference and create two classes like this:
[ResourceProperties(typeof(fooEditor),"Foo Resource",typeof(fooType))]
public class fooHolder:DefaultResourceHolder
{
public fooHolder()
{
}
protected override string resourceTypeName
{
get
{
return "foo Resource";
}
}
protected override object checkResource(object Value)
{
object RetVal = null;
if (Value is fooType)
{
RetVal = base.checkResource(Value);
}
else
{
InvalidCastException up = new InvalidCastException("Value is not foo");
throw up;
}
return RetVal;
}
}
public class fooEditor:DefaultResourceEditor
{
public fooEditor()
{
}
protected override IResourceHolder checkTarget(IResourceHolder Value)
{
IResourceHolder RetVal = null;
if (Value is fooHolder)
{
RetVal = base.checkTarget(Value);
}
else
{
InvalidCastException up =
new InvalidCastException("Value is not a fooHolder");
throw up;
}
return RetVal;
}
protected override void showEditor()
{
frmfooEditor tmp = new frmfooEditor();
tmp.Value = Target.Resource as fooType;
if (tmp.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Target.Resource = tmp.Value;
}
}
}
Now, just add the frmfooEditor form to the project. Design it the way you want it and you have your new Resource Library.
If you want to use your library with the demo project, you create an XML file:
<?xml version="1.0" ?>
<Config>
<LoadLib>myFooLib.dll</LoadLib>
<LoadPath>myLibPath</LoadPath>
</Config>
You have the choice to add only a single library by using LoadLib, or to add all DLLs in a certain directory by using LoadPath.
The XML file must be located in the directory where the executable lies and must be named REConfig.xml.
| You must Sign In to use this message board. | |||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||