How can I close the browser from an XBAP?
Asked Answered
P

3

6

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.

Photochromy answered 31/8, 2009 at 12:50 Comment(1)
+1 to compensate for the unfair -1... that's a good questionPortentous
G
4

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);      
}
Goles answered 31/8, 2009 at 12:55 Comment(2)
I was not able to find HtmlPage in xbap project, tryed to refere system.windows.browser namespace chould not find it.Photochromy
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.Frightful
E
5

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

Engelbert answered 23/5, 2012 at 18:33 Comment(0)
G
4

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);      
}
Goles answered 31/8, 2009 at 12:55 Comment(2)
I was not able to find HtmlPage in xbap project, tryed to refere system.windows.browser namespace chould not find it.Photochromy
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.Frightful
P
1

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();
        }
Punak answered 27/6, 2010 at 7:42 Comment(1)
This doesn't work - all Edge/IE tabs are closed. Is there a better way to just close the tab for current application?Pettifer

© 2022 - 2024 — McMap. All rights reserved.