tags:

views:

429

answers:

2

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  A: 

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);      
}
Svetlozar Angelov
I was not able to find HtmlPage in xbap project, tryed to refere system.windows.browser namespace chould not find it.
Arvind
Thanks That worked.
Arvind
It is more usefull that is sounds to be: A colleague of me needs to prevent several instances of a XBAP application to be launched. Closing the faulty new instance using this method saved his life :) Thanks.
controlbreak
A: 

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();
        }
PrimeTSS