English 中文(简体)
为什么赢得这项工作? - 寻找隐藏的窗口
原标题:why wont this work? - Getting a window that is hidden

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();   

但无论哪一部分填满<代码>。 OfType<MainWindow> or for the MainWindow>, 即使在肯定100%的情况下,也仍然无法返回。

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 ?

页: 1

最佳回答

<代码>Where是多余的,因为你看到它检查了某个具体的名字,这并不涉及你。

问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签