为什么这行不通? - 获取隐藏的窗口
在我的应用程序中,我使用隐藏和可见属性来使窗口如何向用户显示或不向用户显示。 现在在某个时候,当我在 Window1 中时,我想检查是否仍然存在隐藏的主窗口实例。 谷歌搜索,我发现(也在SO上)我需要使用这段代码:
Window deze = Application.Current.Windows.OfType<MainWindow>().Where(x => x.Name == "MainWindow").FirstOrDefault();
但是无论我为 OfType
或 "MainWindow"
部分填写什么,它不断返回 null,即使我 100% 确定必须有 1 个实例。
检查主窗口的代码,在解决方案资源管理器(视觉表达)“mainWindow.xaml”中调用 我看到代码看起来像这样:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//creating the exitmessage for use later on, after all, we want to use this in an other method.
public MainWindow()
{
......
}
我真的完全不知道,为什么当我 100% 确定存在主窗口的隐藏实例时,“Window deze”不断返回 null。
有人对这个有想法吗? 我只是完全忽略了一些逻辑解决方案?
提前致谢!
Within my application, i use the hidden and visible porperties to make windows how or not show to the user.
Now at some point, when i am in Window1, i want to make a check if there is still a instance of the main window, that is hidden.
Googling, i found (also on SO) that i need to use this code:
Window deze = Application.Current.Windows.OfType<MainWindow>().Where(x => x.Name == "MainWindow").FirstOrDefault();
But whatever i fill in for the part OfType<MainWindow>
or for the "MainWindow"
it keeps returning null, even when im 100% sure there must be 1 instance.
Checking in the code of the main window, that is called in the solution explorer (visual express) "mainWindow.xaml"
i see that the code looks like this:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//creating the exitmessage for use later on, after all, we want to use this in an other method.
public MainWindow()
{
......
}
I really have no clue at all, why that "Window deze" keeps returning null when im 100% sure there is a hidden instance of the main window.
Anyone got an idea on this one ?
Im i just totally overlooking some logical solution ?
thanx in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Where
是多余的,正如您所看到的,它会检查与您无关的特定名称。The
Where
is redundant, as you see it checks for a specific name, which does not concern you.试试这个:
您正在做的是查看名为 name 的窗口属性,并查看它是否与字符串“MainWindow”匹配。事实并非如此。由于您已经在寻找这种类型(只有一种),因此您只需将第一个类型返回给您的变量即可。
Try this:
What you are doing is looking at a property of the window called name and seeing if it matches the string "MainWindow". It does not. Since you are already looking for this type (of which there is only one), you just need to return the first one back to your variable.