如何从XBAP关闭浏览器?
我完全信任使用XBAP应用程序。 当我点击按钮时,我需要关闭托管XBAP的浏览器。 我怎样才能做到这一点? Application.Currenty.ShutDown()
仅关闭应用程序,将浏览器留空。
编辑:我的错误,这里是你的问题的一个主题 – 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); }
我知道这是一个非常古老的问题,但是如果有人遇到这个问题,这里有一个更简单的解决方案,只关闭一个标签。
Environment.Exit(0);
来源: 微软论坛
这很棒!但是它也会关闭所有IE,包括任何打开的标签
你不会相信这一个,但是如果你也做了一个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(); }