我想把WINFORM的控件的属性放入XML里,在窗体加载的时候读取XML,获得控件属性,然后对各个空间赋值
因为控件内的好多属性无法序列化,所以我就使用了一个自定义类 [Serializable]
public class ControlModel
{ //Name 获取或设置控件的名称。 string
private string name; public string Name
{
get { return name; }
set { name = value; }
} //Type 获取或设置此控件类型 string
private string type; public string Type
{
get { return type; }
set { type = value; }
}
//自动滚动
//private bool autoScroll; //public bool AutoScroll
//{
// get { return autoScroll; }
// set { autoScroll = value; }
//} //获取或设置在控件中显示的背景颜色。
private MyColor backColor; public MyColor BackColor
{
get { return backColor; }
set { backColor = value; }
} //获取或设置在控件中显示的背景图像。
//private Image backgroundImage; //public Image BackgroundImage
//{
// get { return backgroundImage; }
// set { backgroundImage = value; }
//} //Bottom 获取控件下边缘与其容器的工作区上边缘之间的距离(以像素为单位)。int
private int bottom; public int Bottom
{
get { return bottom; }
set { bottom = value; }
} //CanFocus 获取一个值,该值指示控件是否可以接收焦点。 bool
private bool canFocus; public bool CanFocus
{
get { return canFocus; }
set { canFocus = value; }
} //CanSelect 获取一个值,该值指示是否可以选中控件。 bool
private bool canSelect; public bool CanSelect
{
get { return canSelect; }
set { canSelect = value; }
}
//Controls 获取包含在控件内的控件的集合。 Control.ControlCollection
private ModelCollection controls; public ModelCollection Controls
{
get { return controls; }
set { controls = value; }
} //DefaultBackColor 获取控件的默认背景色。 Color
//private Color defaultBackColor; //public Color DefaultBackColor
//{
// get { return defaultBackColor; }
// set { defaultBackColor = value; }
//} ////DefaultFont 获取控件的默认字体。 Font
//private Font defaultFont; //public Font DefaultFont
//{
// get { return defaultFont; }
// set { defaultFont = value; }
//} ////DefaultForeColor 获取控件的默认前景色。 Color
//private Color defaultForeColor; //public Color DefaultForeColor
//{
// get { return defaultForeColor; }
// set { defaultForeColor = value; }
//} //Dock 获取或设置哪些控件边框停靠到其父控件并确定控件如何随其父级一起调整大小。DockStyle
private DockStyle dock; public DockStyle Dock
{
get { return dock; }
set { dock = value; }
} //Enabled 获取或设置一个值,该值指示控件是否可以对用户交互作出响应。 bool
private bool enabled; public bool Enabled
{
get { return enabled; }
set { enabled = value; }
} //Focused 获取一个值,该值指示控件是否有输入焦点。 bool
private bool focused; public bool Focused
{
get { return focused; }
set { focused = value; }
} //Font 获取或设置控件显示的文字的字体。 Font
private MyFont myfont; public MyFont MyFont
{
get { return myfont; }
set { myfont = value; }
} //ForeColor 获取或设置控件的前景色。 Color
private MyColor foreColor; public MyColor ForeColor
{
get { return foreColor; }
set { foreColor = value; }
} //HasChildren 获取一个值,该值指示控件是否包含一个或多个子控件 bool
private bool hasChildren; public bool HasChildren
{
get { return hasChildren; }
set { hasChildren = value; }
} //Height 获取或设置控件的高度。 int
private int height; public int Height
{
get { return height; }
set { height = value; }
} //Left //获取或设置控件左边缘与其容器的工作区左边缘之间的距离 int
private int left; public int Left
{
get { return left; }
set { left = value; }
} //Location 获取或设置该控件的左上角相对于其容器的左上角的坐标。 Point
private Point location; public Point Location
{
get { return location; }
set { location = value; }
} //Parent 获取或设置控件的父容器。 ControlModel
//private ControlModel parent; //public ControlModel Parent
//{
// get { return parent; }
// set { parent = value; }
//} //Right 获取控件右边缘与其容器的工作区左边缘之间的距离(以像素为单位)。 int
private int right; public int Right
{
get { return right; }
set { right = value; }
} //Size 获取或设置控件的高度和宽度。 Size
private Size size; public Size Size
{
get { return size; }
set { size = value; }
} //TabIndex 获取或设置在控件的容器的控件的 Tab 键顺序。 int
private int tabIndex; public int TabIndex
{
get { return tabIndex; }
set { tabIndex = value; }
} //Text 获取或设置与此控件关联的文本。 string
private string text; public string Text
{
get { return text; }
set { text = value; }
} //Top 获取或设置控件上边缘与其容器的工作区上边缘之间的距离(以像素为单位)。 int
private int top; public int Top
{
get { return top; }
set { top = value; }
} //Visible 获取或设置一个值,该值指示是否显示该控件。 bool
private bool Visible; public bool Visible1
{
get { return Visible; }
set { Visible = value; }
} //Width 获取或设置控件的宽度。 int
private int width; public int Width
{
get { return width; }
set { width = value; }
}
}
下面是MyFont类,用来保存字体:[Serializable]
public class MyFont
{
public MyFont(string fontFamily, float size)
{
this.fontFamily = fontFamily;
this.size = size;
}
private string fontFamily; public string FontFamily
{
get { return fontFamily; }
set { fontFamily = value; }
}
private float size; public float Size
{
get { return size; }
set { size = value; }
}
}
我在使用
XmlSerializer xs = new XmlSerializer(controlmodel.GetType());
xs.Serialize(sw, controlmodel);
的时候总会出现反射类型错误的异常
而我把MyFont注释掉,就一切正常,也能实现序列化
请高手帮我解答一下啊!
因为控件内的好多属性无法序列化,所以我就使用了一个自定义类 [Serializable]
public class ControlModel
{ //Name 获取或设置控件的名称。 string
private string name; public string Name
{
get { return name; }
set { name = value; }
} //Type 获取或设置此控件类型 string
private string type; public string Type
{
get { return type; }
set { type = value; }
}
//自动滚动
//private bool autoScroll; //public bool AutoScroll
//{
// get { return autoScroll; }
// set { autoScroll = value; }
//} //获取或设置在控件中显示的背景颜色。
private MyColor backColor; public MyColor BackColor
{
get { return backColor; }
set { backColor = value; }
} //获取或设置在控件中显示的背景图像。
//private Image backgroundImage; //public Image BackgroundImage
//{
// get { return backgroundImage; }
// set { backgroundImage = value; }
//} //Bottom 获取控件下边缘与其容器的工作区上边缘之间的距离(以像素为单位)。int
private int bottom; public int Bottom
{
get { return bottom; }
set { bottom = value; }
} //CanFocus 获取一个值,该值指示控件是否可以接收焦点。 bool
private bool canFocus; public bool CanFocus
{
get { return canFocus; }
set { canFocus = value; }
} //CanSelect 获取一个值,该值指示是否可以选中控件。 bool
private bool canSelect; public bool CanSelect
{
get { return canSelect; }
set { canSelect = value; }
}
//Controls 获取包含在控件内的控件的集合。 Control.ControlCollection
private ModelCollection controls; public ModelCollection Controls
{
get { return controls; }
set { controls = value; }
} //DefaultBackColor 获取控件的默认背景色。 Color
//private Color defaultBackColor; //public Color DefaultBackColor
//{
// get { return defaultBackColor; }
// set { defaultBackColor = value; }
//} ////DefaultFont 获取控件的默认字体。 Font
//private Font defaultFont; //public Font DefaultFont
//{
// get { return defaultFont; }
// set { defaultFont = value; }
//} ////DefaultForeColor 获取控件的默认前景色。 Color
//private Color defaultForeColor; //public Color DefaultForeColor
//{
// get { return defaultForeColor; }
// set { defaultForeColor = value; }
//} //Dock 获取或设置哪些控件边框停靠到其父控件并确定控件如何随其父级一起调整大小。DockStyle
private DockStyle dock; public DockStyle Dock
{
get { return dock; }
set { dock = value; }
} //Enabled 获取或设置一个值,该值指示控件是否可以对用户交互作出响应。 bool
private bool enabled; public bool Enabled
{
get { return enabled; }
set { enabled = value; }
} //Focused 获取一个值,该值指示控件是否有输入焦点。 bool
private bool focused; public bool Focused
{
get { return focused; }
set { focused = value; }
} //Font 获取或设置控件显示的文字的字体。 Font
private MyFont myfont; public MyFont MyFont
{
get { return myfont; }
set { myfont = value; }
} //ForeColor 获取或设置控件的前景色。 Color
private MyColor foreColor; public MyColor ForeColor
{
get { return foreColor; }
set { foreColor = value; }
} //HasChildren 获取一个值,该值指示控件是否包含一个或多个子控件 bool
private bool hasChildren; public bool HasChildren
{
get { return hasChildren; }
set { hasChildren = value; }
} //Height 获取或设置控件的高度。 int
private int height; public int Height
{
get { return height; }
set { height = value; }
} //Left //获取或设置控件左边缘与其容器的工作区左边缘之间的距离 int
private int left; public int Left
{
get { return left; }
set { left = value; }
} //Location 获取或设置该控件的左上角相对于其容器的左上角的坐标。 Point
private Point location; public Point Location
{
get { return location; }
set { location = value; }
} //Parent 获取或设置控件的父容器。 ControlModel
//private ControlModel parent; //public ControlModel Parent
//{
// get { return parent; }
// set { parent = value; }
//} //Right 获取控件右边缘与其容器的工作区左边缘之间的距离(以像素为单位)。 int
private int right; public int Right
{
get { return right; }
set { right = value; }
} //Size 获取或设置控件的高度和宽度。 Size
private Size size; public Size Size
{
get { return size; }
set { size = value; }
} //TabIndex 获取或设置在控件的容器的控件的 Tab 键顺序。 int
private int tabIndex; public int TabIndex
{
get { return tabIndex; }
set { tabIndex = value; }
} //Text 获取或设置与此控件关联的文本。 string
private string text; public string Text
{
get { return text; }
set { text = value; }
} //Top 获取或设置控件上边缘与其容器的工作区上边缘之间的距离(以像素为单位)。 int
private int top; public int Top
{
get { return top; }
set { top = value; }
} //Visible 获取或设置一个值,该值指示是否显示该控件。 bool
private bool Visible; public bool Visible1
{
get { return Visible; }
set { Visible = value; }
} //Width 获取或设置控件的宽度。 int
private int width; public int Width
{
get { return width; }
set { width = value; }
}
}
下面是MyFont类,用来保存字体:[Serializable]
public class MyFont
{
public MyFont(string fontFamily, float size)
{
this.fontFamily = fontFamily;
this.size = size;
}
private string fontFamily; public string FontFamily
{
get { return fontFamily; }
set { fontFamily = value; }
}
private float size; public float Size
{
get { return size; }
set { size = value; }
}
}
我在使用
XmlSerializer xs = new XmlSerializer(controlmodel.GetType());
xs.Serialize(sw, controlmodel);
的时候总会出现反射类型错误的异常
而我把MyFont注释掉,就一切正常,也能实现序列化
请高手帮我解答一下啊!
解决方案 »
- 实现什么功能
- 关于treeView
- 关于ListBox操作的问题!
- 数据库读取数据转换成枚举
- 如何从DataGrid中得到DataTable,请高手指点
- asp.net下DataGrid数据批量提交问题
- 验证是否是汉字的正则表达式
- 问题:正由另一进程使用,因此该进程无法访问该文件
- dataGridView 显示数据的问题
- 语句
- ASP.NET中在DataGrid同时使用排序与分页
- This event supports the .NET Framework infrastructure and is not intended to be used directly from your code
[XmlInclude(typeof(MyFont))] ------->>>>
public class ControlModel