Problems with hotkeys to open and hide the interface using "Application.Current" (C # WPF)

0

The following code works without problem, I did it based on the code available on the internet of SirMestre, however it only opens / closes the MainWindow UI, I have already tried some things to work but the error or does not work in other UI's.

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;    

namespace Model.Libraries.KeyBoardHooking
{
    public class KeyBoardHooking
    {
        [DllImport("User32.dll")]
        private static extern short GetAsyncKeyState(Key vKey);
        [DllImport("User32.dll")]
        private static extern short GetAsyncKeyState(int vKey);

        public KeyBoardHooking()
        {
            Thread Thread = new Thread(() =>
            {
                while (true)
                {
                    // tecla abrir/fechar (PgUp)
                    if (GetAsyncKeyState(33) == -32767)
                    {
                        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                        {
                            if (Application.Current.MainWindow.Topmost == true)
                            {
                                Application.Current.MainWindow.Hide();
                                Application.Current.MainWindow.Topmost = false;
                            }
                            else
                            {
                                Application.Current.MainWindow.Show();
                                Application.Current.MainWindow.Topmost = true;
                            }
                        }));
                    }
                    Thread.Sleep(1);
                }
            })
            {
                IsBackground = true
            };
            Thread.SetApartmentState(ApartmentState.STA);
            Thread.Start();
        }
    }
}

Function of this code is quite simple, I press PgUp or the key that I configure in "GetAsyncKeyState" that it opens / closes the UI, when I change the "MainWindow" to "Window1" which is another UI in the application it returns a error saying this: "Application" does not contain a setting for "Windows1" and could not find any "Widnows1" extension method that accepts a first argument of type "Application". (That's what you keep getting code on the net hehehe.)

    
asked by anonymous 18.02.2018 / 14:25

1 answer

0

Come on Guilherme,

What happens is that you are misinterpreting the static class Application and consequently the Current property.

  

Application.Current.Window1.Hide ();

This usage is wrong. There is no property Window1 in Application.Current .

To summarize, when you create an instance, you should use the created instance. Example:

Window novaJanela = new Window(); //instancia criada.
novaJanela.Hide(); //executa o método Hide

SOLUTION

Method KeyBoardHooking corrected

public KeyBoardHooking()
        {
            StatusViews Window1 = new StatusViews();

            Thread Thread = new Thread(() =>
            {
                while (true)
                {
                    // tecla abrir/fechar (PgUp)
                    if (GetAsyncKeyState(33) == -32767)
                    {
                        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                        {
                            if (Window1.Topmost == true)
                            {
                                Window1.Hide();
                                Window1.Topmost = false;
                            }
                            else
                            {
                                Window1.Show();
                                Window1.Topmost = true;
                            }
                        }));
                    }
                    Thread.Sleep(1);
                }
            })
            {
                IsBackground = true
            };
            Thread.SetApartmentState(ApartmentState.STA);
            Thread.Start();
        }

Hope it helps. Hugs ...

    
29.03.2018 / 05:47