Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从WPF应用程序访问表单列表_Wpf - Fatal编程技术网

从WPF应用程序访问表单列表

从WPF应用程序访问表单列表,wpf,Wpf,我有一个WPF应用程序,它链接到另一个包含Windows窗体的程序集。我知道在WPF中,我可以使用如下代码遍历打开的WPF窗口列表,但是WPF应用程序中是否有一种方法可以遍历其他程序集中的表单列表 foreach (Window w in App.Current.Windows) { if (window is MyWindow) { // Do something } } 从WPF应用程序中有没有一种方法可以迭代其他程序集的表单列表 foreach (

我有一个WPF应用程序,它链接到另一个包含Windows窗体的程序集。我知道在WPF中,我可以使用如下代码遍历打开的WPF窗口列表,但是WPF应用程序中是否有一种方法可以遍历其他程序集中的表单列表

foreach (Window w in App.Current.Windows)
{
    if (window is MyWindow)
    {
        // Do something
    }
}
从WPF应用程序中有没有一种方法可以迭代其他程序集的表单列表

foreach (Window w in App.Current.Windows)
{
    if (window is MyWindow)
    {
        // Do something
    }
}
其他程序集中的窗体只是类型。您可以使用反射迭代引用程序集的公共类型:

var forms = Assembly
    .GetExecutingAssembly()
    .GetReferencedAssemblies()
    .Select(x => Assembly.Load(x))
    .SelectMany(x => x.GetTypes())
    .Where(x => x == typeof(System.Windows.Forms.Form))
    .ToList();
如果要跟踪当前打开的表单,可以使用某种全局集合,在显示表单时向其中添加表单:

public static class ApplicationService
{
    //add each form that you open to this collection
    public static List<System.Windows.Forms.Form> OpenForms { get; } = new List<System.Windows.Forms.Form>();
}
公共静态类应用程序服务
{
//将打开的每个表单添加到此集合
公共静态列表OpenForms{get;}=new List();
}

请确保在表单关闭时将其从
OpenForms
中删除,否则将阻止其被收集。当然,OpenForms集合应与当前打开的表单保持同步,也就是说,当你打开一个表单时,你会将它添加到集合中,当你关闭它时,你会将它从集合中删除。谢谢你的回复。这看起来很有希望,但是,它似乎返回的是类型列表,而不是表单列表。你能告诉我如何修改它以获得表单吗?对不起,我有点像linq新手。表单只是一个类,您正在创建它的实例,就像其他任何类一样。引用定义了一些表单类的程序集不会神奇地为您创建任何表单实例。所以你的最后一个问题恐怕没有什么意义。是的,我知道在创建表单之前,列表将是空的。但目标是得到一个表单列表,编写的代码返回System.Type列表,我正试图遍历该列表,它给了我一个编译错误。