How can I close the browser from an XBAP?

帅比萌擦擦* 提交于 2019-12-06 03:47:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!