由于项目需求,最近才开始学习c#上的winform开发,自己没有系统的学过,这里我也只是针对自己编程中遇到的问题进行简单分析:
1.在一个工程中调用其它工程中的类
在c#每个工程在编译之后有一个程序集,在工程中加入其它工程的程序集,如果还是不行就加入其他工程的命名空间namespace,例如下面
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
<span style="color:#ff0000;">using JiangSuMobile.AnaxnphwnvsControl;</span>
using System;
namespace JiangSuMobile.Trian.UserManager
{
public class UserManager
{
static public VideoUserInfo videoUserInfo;
public int userLogin(string userName, string userPassword, ref AxnphwnvsLib.Axnphwnvs axnphwnvs1)
{
int iResult = setUserName(userName);
if (iResult < 0)
{
//用户名不合法
return -3;
}
bool bResult = setUserPassword(userPassword);
if (bResult == false)
{
//return -2;
}
if (videoUserInfo == null)
{
videoUserInfo = new VideoUserInfo();
}
videoUserInfo.initVideoUserInfo();
}
}
}
在class UserManager中调用了其它类VideoUserInfo,在该类的前几行中使用了using 语句,使得在
JiangSuMobile.Trian.UserManager命名空间中可以使用JiangSuMobile.AnaxnphwnvsControl命名空间中的类:VideoUserInfo,还需要导入VideoUserInfo所在工程的程序集。
2.接口interface
接口定义一个行为的规范或约定;创建在大范围全异对象间使用的功能;适合为不相干的类提供通用的功能;设计小而简练的模块;
接口用来定义一个功能,由类来实现。
3.Xml的加载、查询、修改
xml的关键字:节点(父节点、子节点),节点列表,属性。(PS:节点也可以称为“元素”)
(1)xml的加载
string xmlfile="xml文件的名字.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.load(xmlfile);
//xml已加载到了xmlDoc中,可以通过操作xmlDoc来进行node的遍历和查询<span style="font-family: Arial, Helvetica, sans-serif;"> </span><span style="font-family: Arial, Helvetica, sans-serif;"> </span>
(2)查询某个节点:还有的根据节点名或者节点属性查询节点的..
XmlNode xmlNode = xmlDocument.SelectSingleNode("/peoplelist/person");//从根节点peoplelist开始查询person节点,查询到后就返回当前节点,这个返回的节点包含了子节点和节点的属性等其他信息,可以通过msdn来查看XmlNode
XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/peoplelist/person");//查询所有<span style="font-family: Arial, Helvetica, sans-serif;">peoplelist下的person节点并构成Nodelist</span>
(3)在指定的xml节点下添加节点:还有根据节点属性添加节点,给节点添加属性等等,这里不一一举例
string xmlFile = "person.xml";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("E:/Document/vsProject/xmlTest/xmlTest/person.xml");
XmlNode xmlNode = xmlDocument.SelectSingleNode("/peoplelist/person");
XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/peoplelist/person");
//在person节点下查找有子节点name为Tim Daly,添加子节点class为092
bool findOut = false;
foreach (XmlNode node in xmlNodeList)
{
XmlElement xmlElement = (XmlElement)node;
foreach (XmlNode childNode in xmlElement.ChildNodes)
{
if (childNode.InnerText.Equals("Tim Daly"))
{
//找到name 为Tim Daly的节点
findOut = true;
break;
}
}
if (findOut == true)
{
XmlElement newElement = xmlDocument.CreateElement("class");
newElement.InnerText = "092";
node.AppendChild(newElement);
xmlDocument.Save("E:/Document/vsProject/xmlTest/xmlTest/person.xml");
}
}
下面是我的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<peoplelist>
<person>
<name>Tom Stafford</name>
<title>CFO</title>
</person>
<person>
<name>Jane Goodwill</name>
<title>CEO</title>
</person>
<person>
<name>Tim Daly</name>
<title>CTO</title>
<title2>CTO0</title2>
<class>092</class>
</person>
<person>
<name>John Graver</name>
<title>CSO</title>
</person>
<aaa>
<bb>
</bb>
</aaa>
</peoplelist>
添加xml节点的方式很多这里只是一种。
(4)xml节点的修改:根据节点名,节点属性修改节点或者属性值,不再举例
4.treeview的使用
关键字:treeview,TreeNode,TreeNode.node, node.Add(),tag
这边贴一下递归调用查询treeview的子节点的代码
public bool bindToSpecialTreeNode(ref TreeNode P_TreeNode_node, string P_str_matchKeyNode, string P_str_key, string P_str_text, int P_str_deviceStatusImage, string P_str_nodeTag)
{
if (P_TreeNode_node.Name == P_str_matchKeyNode)
{
//在当前节点中找到有key值的节点
TreeNode P_TreeNode_child = new TreeNode();
P_TreeNode_child.Tag = P_str_nodeTag;
P_TreeNode_child.Text = P_str_text;
P_TreeNode_child.Name = P_str_key;
P_TreeNode_child.SelectedImageIndex = P_str_deviceStatusImage;
P_TreeNode_child.ImageIndex = P_str_deviceStatusImage;
P_TreeNode_node.Nodes.Add(P_TreeNode_child);
return true;
}
int i = 0;
//当前节点没有找到,继续查找当前节点的子节点,遍历当前节点下的子节点,当该节点下的所有子节点都没有key属性,则继续查找同一级别的节点,知道所有节点遍历完
foreach(TreeNode P_TreeNode_childNode in P_TreeNode_node.Nodes)
{
TreeNode node = P_TreeNode_node.Nodes[i];
if (bindToSpecialTreeNode(ref node, P_str_matchKeyNode, P_str_key, P_str_text, P_str_deviceStatusImage, P_str_nodeTag))
{
return true;
}
i++;
}
return false;
}
5.拿到第三方控件怎么办?
找到控件的属性、方法!!做几个小实例熟悉这个控件!!
6.工程如何布置
和控制面板相关的放在一个工程下,其它与界面无关的程序或者代码——新建类,尽量减少界面间的耦合性,可以的话通过第三方类实现界面间的交互
7.弄清每个.net工程的framWork版本.高版本兼容低版本
讲的比较肤浅....