如何从另一个项目关闭 wpf 窗口 [英] How to close wpf window from another project

查看:53
本文介绍了如何从另一个项目关闭 wpf 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有问题.在项目 A 中,我有一个 wpf,我引用了项目 B,在这个 wpf 中,我创建了一个在项目 B 中定义的 winform b.

I have a problem here. In project A I have a wpf, I referred the project B, and in this wpf , I create a winform b which defined in Project B.

当我关闭表单 b 时,我也想关闭 wpf.

When I closing the form b, I want to also close the wpf.

既然我已经在项目A中引用了项目B,我该如何关闭wpf?

Since I already referred project B in project A. How can I close the wpf?

代码是这样的:

using projectB;
namespace projectA    
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            function();
        }

        public void function()
        {
            form = new formb();
            form.Show();
        }
    }
}

.

namespace projectB
{       
    public partial class formb : WinFormBase
    {
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
            // how to also close the wpf(MainWindow) here?
        }
    }
}

推荐答案

创建一个负责跟踪所有窗口的对象.确保此对象打开所有窗口.

Make an object responsible for keeping track of all the windows. Make sure this object opens all windows.

将所有这些窗口放在一个列表中,只需遍历列表即可关闭所有窗口.

Put all these windows in a list and just walk the list to close all the windows.

使用 Application.Current.Shutdown 可能会导致窗口关闭而不保存状态.

Using Application.Current.Shutdown might cause windows getting closed without saving state.

尝试实现正常关机.

创建一个全局事件并让所有窗口都监听它.引发事件时,让每个窗口自行关闭.

Make a global event and have all the windows listen to it. When the event is raised, have each window close itself.

public static class WindowManager
{
    public static event EventHandler<EventArgs> ClosingWindows;

    public static void CloseWindows()
    {
        var c = ClosingWindows;
        if (c != null)
        {
            c(null, EventArgs.Empty);
        }
    }
}

在每个窗口中注册这个事件并响应它.请注意,您必须决定何时调用 WindowManager.CloseWindows 方法.在这个例子中,它被放在一个按钮的点击处理程序中,但你可能也想把它添加到其他事件处理程序中(当用户按下 Alt-F4 或点击右上角的红色关闭按钮时会发生什么)

In each window register for this event and respond to it. Note that you will have to decide when you call the WindowManager.CloseWindows method. In this example it is put in a click handler of a button but you might want to add it to other event handlers as well (what happens when the user presses Alt-F4 or clicks the red close button on the top right)

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        // register for the other windows closing
        WindowManager.ClosingWindows += WindowManager_ClosingWindows;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowManager.CloseWindows();
    }

    void WindowManager_ClosingWindows(object sender, EventArgs e)
    {
        // when other windows close, close this as well
        this.Close();
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);

        // when closed remove eventhandler
        WindowManager.ClosingWindows -= WindowManager_ClosingWindows;
    }
}

这篇关于如何从另一个项目关闭 wpf 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆