原文轉自:http://www.cnblogs.com/wuhenke/archive/2009/12/10/1621421.html
4.解析Main(string[] args)中參數args。主要是在控制檯中啓動程序時同時賦予了參數的形式。
// ParseArgs may set values that are used elsewhere,
// such as startFullScreen and CurrentSettingsDirectory.
ParseArgs(args);
args中參數可能是:
"worldwind://":加載定位顯示球體某處。
“/f” :全屏啓動。
“/s=……”:指定加載“配置”的文件夾路徑。
這裏要注意的事,Main函數一般是沒有參數的,如果我們以後要寫可以在控制檯下給啓動程序傳入參數,可以借鑑一下。
5.加載上次使用的配置信息,包括上次使用的WorldWind主窗體使用信息和上次使用的World球體顯示信息
加載配置
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->if(CurrentSettingsDirectory == null)
{
// load program settings from default directory
LoadSettings();
World.LoadSettings();
}
else
{
LoadSettings(CurrentSettingsDirectory);
World.LoadSettings(CurrentSettingsDirectory);
}
這裏有幾個知識點可以學習一下:
(1)C#對象的序列化爲XML文件和反序列化Xml文件爲對象,參看:http://www.cnblogs.com/wuhenke/archive/2009/12/10/1621437.html
SettingsBase類的 Load方法中的反序列化:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->XmlSerializer ser = new XmlSerializer(defaultSettings.GetType());
using(TextReader tr = new StreamReader(fileName))
{
settings = (SettingsBase)ser.Deserialize(tr);
settings.m_fileName = fileName; // remember where we loaded from for a later save
}
(2)C#中的StreamWriter類的學習使用
Log類主要用於輸出Error Bug Warning等輸出記錄。主WorldWind.log是保存所有的記錄,然後對Debug信息單獨輸出單個的.log文件。
logWriter負責寫WorldWind.log
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->logPath = DefaultSettingsDirectory();
Directory.CreateDirectory(logPath);
// TODO: do not hardcode logfile name?
logFilePath = Path.Combine( logPath, "WorldWind.log" );
logWriter = new StreamWriter(logFilePath, true);
logWriter.AutoFlush = true;
public static void Write( Exception caught )負責寫單獨每個Error的輸出。
單個Log文件輸出
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->string functionName = "Unknown";
string[] stacktrace = null;
if (caught.StackTrace != null)
{
stacktrace = caught.StackTrace.Split('\n');
string firstStackTraceLine = stacktrace[0];
functionName = firstStackTraceLine.Trim().Split(" (".ToCharArray())[1];
}
string logFileName = string.Format("DEBUG_{0}.txt", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff")); //以DEBUG+日期作爲文件名
string logFullPath = Path.Combine(logPath, logFileName);
using (StreamWriter sw = new StreamWriter(logFullPath, false))
{
sw.WriteLine(caught.ToString());
}
6.啓動主程序,還有對線程異常事件處理和程序空閒處理(防止過度休眠)
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
MainApplication app = new MainApplication();
Application.Idle += new EventHandler(app.WorldWindow.OnApplicationIdle);
Application.Run(app);
7.程序啓動後,將狀態配置保存起來
// Save World settings
World.Settings.Save();
其中用到了,將World的配置對象WorldSettings(注意:不是其父類SettingsBase,只是調用其父類的Save方法)序列化爲XML文件,並保存,以備下次啓動前讀取World(即球體)上次狀態配置。
將WorldSettings序列化
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> public virtual void Save(string fileName)
{
XmlSerializer ser = null;
try
{
ser = new XmlSerializer(this.GetType());
using(TextWriter tw = new StreamWriter(fileName))
{
ser.Serialize(tw, this);
}
}
catch(Exception ex)
{
throw new System.Exception(String.Format("Saving settings class '{0}' to {1} failed", this.GetType().ToString(), fileName), ex);
}
}
8.保存程序整體的該次狀態配置。(此處類似7,不詳講,只是這次序列化的對象是WorldWindSettings)
// Save program settings
Settings.Save();
總結:主函數內容分析學習到此完成。我們之後會針對各功能分別分析,個個突破。