可以在VS设计视图下加载出XML多语言配置文件的多语言控件

闲着没事写点东西,可用于WINFORM适当修改下可用在WEB下的多语言控件,在VS设计视图下可自动加载出xml中所配置的语言节点。
  1public class LanguageText : Label, IPropertyGridable
  2    {
  3        public enum LanguageType
  4        
  5            CN=0,
  6            EN=1,
  7        }

  8        XmlDocument doc = new XmlDocument();
  9        string nodeValue;
 10        string xml;
 11        /// <summary>
 12        /// XML内容
 13        /// </summary>

 14        private string XMLString
 15        {
 16            get
 17            {
 18                if (Language == LanguageType.CN)
 19                {
 20                    if (CnLanguageXmlFile != null && CnLanguageXmlFile != string.Empty)
 21                    {
 22                        doc.Load(CnLanguageXmlFile);
 23                    }

 24                }

 25                else
 26                {
 27                    if (EnLanguageXmlFile != null && EnLanguageXmlFile != string.Empty)
 28                    {
 29                        doc.Load(EnLanguageXmlFile);
 30                    }

 31                }

 32                return doc.InnerXml==string.Empty?xml:doc.InnerXml;
 33            }

 34            set
 35            {
 36                xml = value;
 37            }

 38        }

 39        /// <summary>
 40        /// 
 41        /// </summary>

 42        [Category("Language")]
 43        public string NodeValue
 44        {
 45            get return nodeValue; }
 46            set { nodeValue = value; Text = value; }
 47        }

 48        string nodeName = "ProductName";
 49        /// <summary>
 50        /// XML文件中节点列表,绑定LangugeXMLConverter实现VS属性窗口节点选择
 51        /// </summary>

 52        [Category("Language"), TypeConverter(typeof(LangugeXMLConverter))]     
 53        public string NodeName
 54        {
 55            get return nodeName; }
 56            set { nodeName = value; NodeNameChange(value); }
 57        }

 58        public LanguageText()
 59        {
 60            NodeNameChange(NodeName);
 61        }

 62        public override string Text
 63        {
 64            get
 65            {
 66                return NodeValue;
 67            }

 68            set base.Text = value; }
 69        }

 70        /// <summary>
 71        /// 改变节点时并改变相应的值
 72        /// </summary>
 73        /// <param name="name"></param>

 74        public void NodeNameChange(string name)
 75        {
 76            nodeName = name;
 77            if (XMLString!=null&&XMLString != string.Empty)
 78            {
 79                doc.LoadXml(XMLString);
 80                if (doc.GetElementsByTagName(NodeName).Count > 0)
 81                {
 82                    NodeValue = doc.GetElementsByTagName(NodeName).Item(0).FirstChild.Value;
 83                }

 84            }

 85        }

 86        /// <summary>
 87        /// 获取XML中的配置节点
 88        /// </summary>
 89        /// <returns></returns>

 90        public string[] GetNodes()
 91        {
 92            if (XMLString == null || XMLString == string.Empty)
 93            
 94                return new string[]{"加载XML失败"};
 95            }

 96            doc.LoadXml(XMLString);
 97            XmlNodeList nodes = (doc.DocumentElement).ChildNodes;
 98            List<string> lst = new List<string>();
 99            foreach (XmlNode item in nodes)
100            {
101                lst.Add(item.Name);
102            }

103            return lst.ToArray();
104        }

105        
106        string enLanguageXmlFile = "";
107        /// <summary>
108        /// 英文XML配置文件
109        /// </summary>

110        [Category("Language"),EditorAttribute(typeof(OpenFileDialogEditor), typeof(System.Drawing.Design.UITypeEditor))]
111        public string EnLanguageXmlFile
112        {
113            get return enLanguageXmlFile; }
114            set { enLanguageXmlFile = value; }
115        }

116        string cnLanguageXmlFile = "";
117        /// <summary>
118        /// 中文XML配置文件
119        /// </summary>

120        [Category("Language"),EditorAttribute(typeof(OpenFileDialogEditor), typeof(System.Drawing.Design.UITypeEditor))]
121        public string CnLanguageXmlFile
122        {
123            get return cnLanguageXmlFile; }
124            set { cnLanguageXmlFile = value; }
125        }

126        LanguageType language;
127        /// <summary>
128        /// 语言类型
129        /// </summary>

130        [Category("Language")]
131        public LanguageType Language
132        {
133            get return language; }
134            set { language = value; NodeNameChange(NodeName); }
135        }

136        
137    }

138    public class OpenFileDialogEditor : System.Drawing.Design.UITypeEditor
139    {
140        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
141        {
142            OpenFileDialog fileD = new OpenFileDialog();
143            fileD.CheckFileExists = true;
144            fileD.CheckPathExists = true;
145            if (fileD.ShowDialog() == DialogResult.OK)
146            {
147                return fileD.FileName;
148            }

149            return "";
150        }

151        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
152        {
153            return System.Drawing.Design.UITypeEditorEditStyle.Modal;
154        }

155    }

156    public interface IPropertyGridable
157    {
158        string[] GetNodes();
159    }

160    /// <summary>
161    /// 将XML数据转换成字符串数组。实现VS中属性窗口视图化
162    /// </summary>

163    public class LangugeXMLConverter : TypeConverter
164    {
165        public override bool GetStandardValuesSupported(
166        ITypeDescriptorContext context)
167        {
168            return true;
169        }

170        public override bool GetStandardValuesExclusive(
171        ITypeDescriptorContext context)
172        {
173            return false;
174        }

175        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
176        {
177            IPropertyGridable instance = (IPropertyGridable)context.Instance;
178            StandardValuesCollection vals = new TypeConverter.StandardValuesCollection(instance.GetNodes());
179            return vals;
180        }

181        public override bool IsValid(ITypeDescriptorContext context, object value)
182        {
183            return false;
184        }

185    }
 
posted @ 2009-04-03 10:21  Tenny  阅读(791)  评论(1编辑  收藏  举报