2009-08-31 154 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