注意:這是一份提供WPF外部瀏覽器打開html的方法,而不是WPF內部嵌入html
需要通過瀏覽器打開,否則無法使用地址欄拼接參數的形式操作html
下面是打開html的方法↓
string localHtmlPath = @"C:\Users\pangb\Downloads\Help\幫助文檔 - 副本.html";if (File.Exists(localHtmlPath)){try{string browserPath = GetDefaultBrowserPath();string fullUri = new Uri(localHtmlPath).AbsoluteUri + "?page=first.html";Process.Start(browserPath, $"\"{fullUri}\"");}catch (Exception ex){MessageBox.Show($"打開文件時出錯: {ex.Message}");}}else{MessageBox.Show("HTML文件未找到!");}
這是獲取瀏覽器的方法↓
private string GetDefaultBrowserPath(){try{string name = Microsoft.Win32.Registry.GetValue(@"HKEY_CLASSES_ROOT\http\shell\open\command", "", null) as string;if (name != null){// 清理路徑(移除參數和引號)if (name.Contains("\""))name = name.Substring(1, name.IndexOf('"', 1) - 1);return name;}}catch{// 忽略錯誤,返回空字符串}return null;}