为什么这行不通? - 获取隐藏的窗口

发布于 2024-12-27 04:34:23 字数 947 浏览 1 评论 0原文

在我的应用程序中,我使用隐藏和可见属性来使窗口如何向用户显示或不向用户显示。 现在在某个时候,当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹏半生如梦愿梦如真 2025-01-03 04:34:23

Where 是多余的,正如您所看到的,它会检查与您无关的特定名称。

The Where is redundant, as you see it checks for a specific name, which does not concern you.

梦途 2025-01-03 04:34:23

试试这个:

Window deze = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();

您正在做的是查看名为 name 的窗口属性,并查看它是否与字符串“MainWindow”匹配。事实并非如此。由于您已经在寻找这种类型(只有一种),因此您只需将第一个类型返回给您的变量即可。

Try this:

Window deze = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文