Winforms 是否可以在运行时将元数据嵌入Winform或控件中?

Winforms 是否可以在运行时将元数据嵌入Winform或控件中?,winforms,Winforms,MS Access和VB具有窗体和控件的标记属性,您可以在运行时将其存储在其中并与应用程序一起持久化。在.NET中是否有类似的功能?在.NET中,大多数UI控件都有一个可用于相同目的的.Tag属性 但是,如果您需要其他功能,您可以创建一个继承自基本控件的类(即,您可以创建一个名为SpecialPictureBox的类,该类继承自PictureBox并添加其他字段),并在Windows窗体中使用它,就像使用PictureBox一样。在.NET中,大多数UI控件都有一个.Tag属性,可以用于相同的目

MS Access和VB具有窗体和控件的标记属性,您可以在运行时将其存储在其中并与应用程序一起持久化。在.NET中是否有类似的功能?

在.NET中,大多数UI控件都有一个可用于相同目的的
.Tag
属性


但是,如果您需要其他功能,您可以创建一个继承自基本控件的类(即,您可以创建一个名为SpecialPictureBox的类,该类继承自PictureBox并添加其他字段),并在Windows窗体中使用它,就像使用PictureBox一样。

在.NET中,大多数UI控件都有一个
.Tag
属性,可以用于相同的目的


但是,如果您需要其他功能,您可以创建一个继承自基本控件的类(即,您可以创建一个名为SpecialPictureBox的类,该类继承自PictureBox并添加其他字段),并在Windows窗体中使用它,就像使用PictureBox一样。

Windows窗体中有一个标记属性,但它不会持久化

我使用的模式是创建一个名为Preferences的类(具有我想要持久化的每一条信息的属性),然后使用PreferencesManager类管理该类,如下所示:

    public class PreferencesManager
{
    static private string dataPath = null;
    static public string DataPath
    {
        get
        {
            if (dataPath == null)
            {
                string baseFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                dataPath = Path.Combine(baseFolder, @"MY_APP_NAME");

                if (!Directory.Exists(dataPath))
                {
                    Directory.CreateDirectory(dataPath);
                }
            }

            return dataPath;
        }
    }

    /// <summary>
    /// Saves the specified preferences.
    /// </summary>
    /// <param name="pref">The preferences.</param>
    static public void Save(Preferences pref)
    {
        // Create file to save the data to

        string fn = "Preferences.xml";
        string path = Path.Combine(DataPath, fn);
        using (FileStream fs = new FileStream(path, FileMode.Create))
        {

            // Create an XmlSerializer object to perform the serialization
            XmlSerializer xs = new XmlSerializer(typeof(Preferences));

            // Use the XmlSerializer object to serialize the data to the file
            xs.Serialize(fs, pref);
        }
    }

    static public Preferences Load()
    {
        Preferences ret = null;
        string path = string.Empty;

        try
        {
            // Open file to read the data from

            string fn = "Preferences.xml";
            path = Path.Combine(DataPath, fn);
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {

                // Create an XmlSerializer object to perform the deserialization
                XmlSerializer xs = new XmlSerializer(typeof(Preferences));

                // Use the XmlSerializer object to deserialize the data from the file
                ret = (Preferences)xs.Deserialize(fs);
            }
        }
        catch (System.IO.DirectoryNotFoundException)
        {
            throw new Exception("Could not find the data directory '" + DataPath + "'");
        }
        catch (InvalidOperationException)
        {
            return new Preferences();
        }
        catch (System.IO.FileNotFoundException)
        {
            return new Preferences();
        }

        return ret;
    }

}
公共类首选项管理器
{
静态私有字符串dataPath=null;
静态公共字符串数据路径
{
得到
{
如果(数据路径==null)
{
string baseFolder=System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
dataPath=Path.Combine(baseFolder,@“我的应用程序名称”);
如果(!Directory.Exists(dataPath))
{
CreateDirectory(数据路径);
}
}
返回数据路径;
}
}
/// 
///保存指定的首选项。
/// 
///偏好。
静态公共作废保存(首选项优先)
{
//创建要将数据保存到的文件
字符串fn=“Preferences.xml”;
字符串路径=path.Combine(DataPath,fn);
使用(FileStream fs=newfilestream(路径,FileMode.Create))
{
//创建XmlSerializer对象以执行序列化
XmlSerializer xs=新的XmlSerializer(typeof(首选项));
//使用XmlSerializer对象将数据序列化到文件
序列化(fs,pref);
}
}
静态公共首选项加载()
{
首选项ret=null;
字符串路径=string.Empty;
尝试
{
//打开要从中读取数据的文件
字符串fn=“Preferences.xml”;
path=path.Combine(数据路径,fn);
使用(FileStream fs=newfilestream(路径,FileMode.Open))
{
//创建XmlSerializer对象以执行反序列化
XmlSerializer xs=新的XmlSerializer(typeof(首选项));
//使用XmlSerializer对象反序列化文件中的数据
ret=(首选项)xs.反序列化(fs);
}
}
捕获(System.IO.DirectoryNotFoundException)
{
抛出新异常(“找不到数据目录“'+DataPath+””);
}
捕获(无效操作异常)
{
返回新的首选项();
}
捕获(System.IO.FileNotFoundException)
{
返回新的首选项();
}
返回ret;
}
}

Windows窗体中有一个标记属性,但它不会持久化

我使用的模式是创建一个名为Preferences的类(具有我想要持久化的每一条信息的属性),然后使用PreferencesManager类管理该类,如下所示:

    public class PreferencesManager
{
    static private string dataPath = null;
    static public string DataPath
    {
        get
        {
            if (dataPath == null)
            {
                string baseFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                dataPath = Path.Combine(baseFolder, @"MY_APP_NAME");

                if (!Directory.Exists(dataPath))
                {
                    Directory.CreateDirectory(dataPath);
                }
            }

            return dataPath;
        }
    }

    /// <summary>
    /// Saves the specified preferences.
    /// </summary>
    /// <param name="pref">The preferences.</param>
    static public void Save(Preferences pref)
    {
        // Create file to save the data to

        string fn = "Preferences.xml";
        string path = Path.Combine(DataPath, fn);
        using (FileStream fs = new FileStream(path, FileMode.Create))
        {

            // Create an XmlSerializer object to perform the serialization
            XmlSerializer xs = new XmlSerializer(typeof(Preferences));

            // Use the XmlSerializer object to serialize the data to the file
            xs.Serialize(fs, pref);
        }
    }

    static public Preferences Load()
    {
        Preferences ret = null;
        string path = string.Empty;

        try
        {
            // Open file to read the data from

            string fn = "Preferences.xml";
            path = Path.Combine(DataPath, fn);
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {

                // Create an XmlSerializer object to perform the deserialization
                XmlSerializer xs = new XmlSerializer(typeof(Preferences));

                // Use the XmlSerializer object to deserialize the data from the file
                ret = (Preferences)xs.Deserialize(fs);
            }
        }
        catch (System.IO.DirectoryNotFoundException)
        {
            throw new Exception("Could not find the data directory '" + DataPath + "'");
        }
        catch (InvalidOperationException)
        {
            return new Preferences();
        }
        catch (System.IO.FileNotFoundException)
        {
            return new Preferences();
        }

        return ret;
    }

}
公共类首选项管理器
{
静态私有字符串dataPath=null;
静态公共字符串数据路径
{
得到
{
如果(数据路径==null)
{
string baseFolder=System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
dataPath=Path.Combine(baseFolder,@“我的应用程序名称”);
如果(!Directory.Exists(dataPath))
{
CreateDirectory(数据路径);
}
}
返回数据路径;
}
}
/// 
///保存指定的首选项。
/// 
///偏好。
静态公共作废保存(首选项优先)
{
//创建要将数据保存到的文件
字符串fn=“Preferences.xml”;
字符串路径=path.Combine(DataPath,fn);
使用(FileStream fs=newfilestream(路径,FileMode.Create))
{
//创建XmlSerializer对象以执行序列化
XmlSerializer xs=新的XmlSerializer(typeof(首选项));
//使用XmlSerializer对象将数据序列化到文件
序列化(fs,pref);
}
}
静态公共首选项加载()
{
首选项ret=null;
字符串路径=string.Empty;
尝试
{
//打开要从中读取数据的文件
字符串fn=“Preferences.xml”;
path=path.Combine(数据路径,fn);
使用(FileStream fs=newfilestream(路径,FileMode.Open))
{
//创建XmlSerializer对象以执行反序列化
XmlSerializer xs=新的XmlSerializer(typeof(首选项));
//使用XmlSerializer对象反序列化文件中的数据
ret=(首选项)xs.反序列化(fs);
}
}
捕获(System.IO.DirectoryNotFoundException)
{
抛出新异常(“无法找到d