C# 获取从不同程序集执行的打开窗口窗体实例列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/388859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get list of open windows form instance that are excuted from different assembly
提问by abmv
I have a 'loader app' that loads a menu and when user clicks the menu image button a list view opens based on the text
我有一个加载菜单的“加载器应用程序”,当用户单击菜单图像按钮时,会根据文本打开一个列表视图
(if text = employee)
(Go to class A)
(Go to class B)
...
...
(Show List View Window)
if he clicks again on the same button it opens again, I would like to prevent this. i.e but this for a WPF application
如果他再次单击它再次打开的同一个按钮,我想防止这种情况发生。即但这是一个 WPF 应用程序
回答by Marc Gravell
If you want a list of the open forms, that is Application.OpenForms
. You could iterate over this, using GetType() and checking the .Assembly
to find those from a different assembly. Beyond that, I'm not entire clear on the question...
如果您想要一个打开表单的列表,那就是Application.OpenForms
. 你可以迭代这个,使用 GetType() 并检查.Assembly
以从不同的程序集中找到那些。除此之外,我对这个问题并不完全清楚......
Assembly currentAssembly = Assembly.GetExecutingAssembly();
List<Form> formsFromOtherAssemblies = new List<Form>();
foreach (Form form in Application.OpenForms) {
if (form.GetType().Assembly != currentAssembly) {
formsFromOtherAssemblies.Add(form);
}
}
If you just want to track forms you have opened yourself, then cache that instance. Or if you use "owned forms", you can just check by name:
如果您只想跟踪自己打开的表单,请缓存该实例。或者,如果您使用“拥有的表单”,则只需按名称检查:
private void button1_Click(object sender, EventArgs e) {
foreach (Form form in OwnedForms) {
if (form.Name == "Whatever") {
form.Activate();
return;
}
}
Form child = new Form();
child.Name = "Whatever";
child.Owner = this;
child.Show(this);
}
回答by TcKs
You can use a Command pattern. The loader assembly will search for commands in loaded assemblies. For every command the loader create menu item ( or anything else, you want ), and click event will run the concrete command.
您可以使用命令模式。加载程序集将在加载的程序集中搜索命令。对于每个命令,加载程序都会创建菜单项(或其他任何你想要的),并且单击事件将运行具体命令。
The command must know if should be created new form or used some already existing.
该命令必须知道是否应该创建新表单或使用一些已经存在的表单。
回答by joakim
NewProduct newproduct;
private void button1_Click(object sender, EventArgs e)
{
if(!isOpened())
{
newproduct = new NewProduct();
newproduct.Show();
}
}
private bool isOpened()
{
foreach (Form f in Application.OpenForms)
{
if (f == newproduct)
{
return true;
}
}
return false;
}
回答by Matt Ellen
Mark Garvell's answer helped me to figure out what I should do, but it needed adjusting for WPF.
Mark Garvell 的回答帮助我弄清楚我应该做什么,但它需要针对 WPF 进行调整。
(In my case I wanted to close any windows not owned by the main one when it closes, but the principle is the same.)
(在我的情况下,我想在关闭时关闭不属于主窗口的所有窗口,但原理是相同的。)
private void EmployeeMenuItemClick(object sender, RoutedEventArgs e)
{
bool found = false;
foreach(Window w in Application.Current.Windows)
{
if(w.GetType() == typeof(EmployeeListViewWindow)
{
found = true;
break;
}
}
if(!found)
{
EmployeeListViewWindow ew = new EmployeeListViewWindow();
ew.Show();
}
}
回答by daniele3004
Another simple example
另一个简单的例子
private Boolean FindForm(String formName)
{
foreach (Form f in Application.OpenForms)
{
if (f.Name.Equals(formName))
{
f.Location = new Point(POINT.X, POINT.Y + 22);
return true;
}
}
return false;
}