问题
I am using an XBAP application in full trust. I need to close the browser hosting the XBAP when I click a button. How can I achieve this? Application.Currenty.ShutDown()
only closes the application, leaving the browser blank.
回答1:
EDIT: My mistake, here is a thread with your problem - http://social.msdn.microsoft.com/forums/en-US/wpf/thread/21c88fed-c84c-47c1-9012-7c76972e8c1c
and to be more specific (this code needs full trust security settings)
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);
}
回答2:
I know this is a really old question, but in case anyone has this issue, here's a simpler solution that will close just the one tab.
Environment.Exit(0);
Source: Microsoft Forums
回答3:
This is great!, however it also shuts down all of IE, including any open tabs
Your not going to believe this one, but if you also do a Application.Current.Shutdown(): after above it aborts the total IE shutdown and just shuts the applications tab.
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();
}
来源:https://stackoverflow.com/questions/1357308/how-can-i-close-the-browser-from-an-xbap