/// <summary> /// 设置对象所对应的资源文件 /// </summary> /// <param name="obj">将要设置的对象</param> static public void SetObjectResourcesHelper(object obj) { //键值:即为对象的类型的全名 string keyForObject = obj.GetType().FullName; //资源文件存储的路径 string filePath = Path.Combine(FullDirectory, keyForObject + ".formXml"); #if DEBUG /// 当Debug状态时,如果配置文件不存在,则自动生成一个配置文件 if (!File.Exists(filePath)) { ResourceXmlFile.ObjectXmlCreator(obj, filePath); } #endif }
/// <summary> /// 初始化资源。一般在应用程序启动时调用。 /// </summary> /// <param name="fullDirectory">资源目录完整名。</param> /// <param name="languageDirectory">语言代码:chs,ths,en...。</param> /// <param name="type">类型名</param> static public void InitializeResources(string fullDirectory, Type type) { FullDirectory = fullDirectory; if (type == null) { type = typeof(ResourcesReader); } #if DEBUG /// 当Debug状态时,如果配置文件不存在,则自动生成一个配置文件 string filePath = Path.Combine(fullDirectory, type.FullName + ".formXml"); if (!File.Exists(filePath)) { ResourceXmlFile.PublicXmlCreator(type, filePath); } #endif /// 初始化应用程序的公共资源 _textPublicResources = TextReader(type); _iconPublicResources = IconReader(type); _imagePublicResources = ImageReader(type); _cursorPublicResources = CursorReader(type); _byteArrayPublicResources = ByteArrayReader(type); }
/// <summary> /// 获取根据控件类型获取资源Dictionary。 /// </summary> /// <param name="ctr">控件对象</param> /// <param name="textRes">文本资源Dictionary</param> /// <param name="iconRes">图标资源Dictionary</param> /// <param name="imageRes">图像资源Dictionary</param> static void ReaderForControl(Control ctr, out Dictionary <string, string> textRes, out Dictionary <string, Icon> iconRes, out Dictionary <string, Image> imageRes) { //键值:即为控件的类型的全名 string keyForType = ctr.GetType().FullName; //资源文件存储的路径 string filePath = Path.Combine(FullDirectory, keyForType + ".formXml"); //从整个应用程序的总的Dictionary中获取该类型的Dictionary,如果不存在,则读取资源 //文件生成该Dictionary并存入总的Dictionary if (!_controlTextResources.TryGetValue(keyForType, out textRes)) { textRes = new Dictionary <string, string>(); iconRes = new Dictionary <string, Icon>(); imageRes = new Dictionary <string, Image>(); #if DEBUG /// 当Debug状态时,如果配置文件不存在,则自动生成一个配置文件 if (!File.Exists(filePath)) { ResourceXmlFile.XmlCreator(ctr, filePath); } #endif XmlDocument doc = new XmlDocument(); doc.Load(filePath); #region 遍历XmlNode,将关于Control的配置存储在静态Dictionary中 foreach (XmlNode node in doc.DocumentElement.ChildNodes) { if (node.NodeType != XmlNodeType.Element) { continue; } if (node.Name == "Control" || node.Name == "ToolStripItem") { XmlNode textNode = node.SelectSingleNode("Text"); if (textNode != null) { string str = textNode.InnerText; if (!string.IsNullOrEmpty(str)) { textRes.Add(node.Attributes["Name"].Value, str); } } XmlNode iconNode = node.SelectSingleNode("Icon"); if (iconNode != null) { string str = iconNode.InnerText; if (!string.IsNullOrEmpty(str)) { Icon icon = Jeelu.Utility.Convert.Base64ToIcon(str); iconRes.Add(node.Attributes["Name"].Value, icon); } } XmlNode imageNode = node.SelectSingleNode("Image"); if (imageNode != null) { string str = imageNode.InnerText; if (!string.IsNullOrEmpty(str)) { Image image = Jeelu.Utility.Convert.Base64ToImage(str); imageRes.Add(node.Attributes["Name"].Value, image); } } } } #endregion _controlTextResources.Add(keyForType, textRes); _controlImageResources.Add(keyForType, imageRes); _controlIconResources.Add(keyForType, iconRes); } else { iconRes = _controlIconResources[keyForType]; imageRes = _controlImageResources[keyForType]; } }