2009-08-31 153 views
4

我以完全信任的方式使用XBAP應用程序。當我點擊一個按鈕時,我需要關閉託管XBAP的瀏覽器。我怎樣才能做到這一點? Application.Currenty.ShutDown()只關閉應用程序,使瀏覽器空白。如何從XBAP關閉瀏覽器?

+1

+1,以補償不公平-1 ...這是一個很好的問題 – 2009-08-31 14:23:34

回答

3

編輯: 我的錯誤,這裏是你的問題的一個話題 - http://social.msdn.microsoft.com/forums/en-US/wpf/thread/21c88fed-c84c-47c1-9012-7c76972e8c1c

和更具體的(此代碼需要完全信任安全設置)

using System.Windows.Interop; 
using System.Runtime.InteropServices; 

[DllImport("user32", ExactSpelling = true, CharSet = CharSet.Auto)] 
private static extern IntPtr GetAncestor(IntPtr hwnd, int flags); 

[DllImport("user32", CharSet = CharSet.Auto)] 
private static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam); 

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
     WindowInteropHelper wih = new WindowInteropHelper(Application.Current.MainWindow); 
     IntPtr ieHwnd = GetAncestor(wih.Handle, 2); 
     PostMessage(ieHwnd, 0x10, IntPtr.Zero, IntPtr.Zero);  
} 
+0

我沒能找到HtmlPage在XBAP項目,tryed來參考,system.windows.browser命名空間無法找到它。 – Arvind 2009-08-31 13:04:54

+0

謝謝你工作。 – Arvind 2009-08-31 13:39:51

+0

這聽起來更有用:我的一位同事需要阻止幾個XBAP應用程序的實例啓動。使用這種方法關閉錯誤的新實例挽救了他的生命:)謝謝。 – Larry 2009-09-10 13:57:25

1

這是不過它也關閉了所有的IE,包括任何公開的標籤e,但是如果您還執行Application.Current.Shutdown():在上面之後它會中止完全的IE關閉並關閉應用程序選項卡。

private void exitButton_Click(object sender, RoutedEventArgs e) 
     { 
      // This will Shut entire IE down 
      WindowInteropHelper wih = new WindowInteropHelper(Application.Current.MainWindow); 
      IntPtr ieHwnd = GetAncestor(wih.Handle, 2); 
      PostMessage(ieHwnd, 0x10, IntPtr.Zero, IntPtr.Zero);  

      // Singularly will just shutdown single tab and leave white screen, however with above aborts the total IE shutdown 
      // and just shuts the current tab 
      Application.Current.Shutdown(); 
     } 
5

我知道這是一個非常古老的問題,但如果任何人有這個問題,這裏有一個簡單的解決方案,將關閉只是一個標籤。

Environment.Exit(0); 

來源:Microsoft Forums