Przeglądaj źródła

improve the XmlResourceData class.

gsp 3 lat temu
rodzic
commit
3841f13f26

+ 42 - 6
OTSIncAMeasureApp/0-OTSModel/OTSDataType/XmlResourceData.cs

@@ -1,4 +1,5 @@
 using System.Collections.Generic;
+using System.IO;
 using System.Xml;
 
 namespace OTSDataType
@@ -27,40 +28,75 @@ namespace OTSDataType
         }
         public void GetStringByKey(int grpKey, int itmKey, ref string text, ref string des)
         {
+            if (!resGroup.ContainsKey(grpKey))
+            {
+                return;
+            }
+            if (!resGroup[grpKey].mapRes.ContainsKey(itmKey))
+            {
+                return;
+            }
             text = resGroup[grpKey].mapRes[itmKey].text;
             des = resGroup[grpKey].mapRes[itmKey].Description;
         }
         public string GetStringByKey(int grpKey, int itmKey)
         {
+            if (!resGroup.ContainsKey(grpKey))
+            {
+                return "";
+            }
+            if (!resGroup[grpKey].mapRes.ContainsKey(itmKey))
+            {
+                return "";
+            }
             return resGroup[grpKey].mapRes[itmKey].text;
         }
-        public string GetStringByKey(int itmKey)
-        {
-            return resGroup[0].mapRes[itmKey].text;
-        }
+      
         public string GetGroupTextByKey(int grpKey)
         {
+            if (!resGroup.ContainsKey(grpKey))
+            {
+                return "";
+            }
             return resGroup[grpKey].text;
         }
 
         public void GetGroupTextByKey(int grpKey, ref string text, ref string des)
         {
+            if (!resGroup.ContainsKey(grpKey))
+            {
+                return ;
+            }
             text = resGroup[grpKey].text;
             des = resGroup[grpKey].Description;
         }
 
-        public void SetStringByKey(int grpkey, int itmkey, string value, string des)
+        public void SetStringByKey(int grpKey, int itmKey, string value, string des)
         {
+            if (!resGroup.ContainsKey(grpKey))
+            {
+                return;
+            }
+            if (!resGroup[grpKey].mapRes.ContainsKey(itmKey))
+            {
+                return;
+            }
             StringRes sr = new StringRes();
             sr.text = value;
             sr.Description = des;
-            resGroup[grpkey].mapRes[itmkey] = sr;
+            resGroup[grpKey].mapRes[itmKey] = sr;
         }
 
         public bool LoadStringFromXml()
         {
             XmlDocument xml = new XmlDocument();
+            if (!Directory.Exists(".\\Resources\\XMLData\\LanguageDefine.xml"))
+            {
+                NLog.LogManager.GetCurrentClassLogger().Error("There's no \\Resources\\XMLData\\LanguageDefine.xml");
+                return false;
+            }
             xml.Load(".\\Resources\\XMLData\\LanguageDefine.xml");
+
             XmlNode root = xml.SelectSingleNode("Language");
             XmlNode root2 = root.SelectSingleNode("DefaultLanguage");
             string ss = root2.InnerText;

+ 1 - 1
OTSIncAMeasureApp/ResourceManage/ResourceData.cs

@@ -344,7 +344,7 @@ namespace OTSModelSharp.ResourceManage
         }
         public static string GetResourceByKey(int resourceGrp, int Index)
         {
-
+            
             string strSource = XmlResourceData.GetInstance().GetStringByKey(resourceGrp, Index);
             return strSource;
         }