Brainy_Clown
Brainy_Clown

Reputation: 82

Window open on Notification click

I have created a chat application, so when I received a message there is a notification generated so that on clicking notification my chat application should open and I did it using this code below

if (!ApplicationContext.ContactsViewModel.IsWindowOpen)
  {
     ApplicationContext.CurrentChatView.Dispatcher.Invoke(() =>
     {
        ApplicationContext.CurrentChatView.WindowState = WindowState.Normal;
        ApplicationContext.CurrentChatView.Activate();
     });
  }

so the problem here is my application is performing all the tasks in the background but instead of appearing in the foreground

I have also tried :

ApplicationContext.CurrentChatView.Topmost=true;

but in this case, the application remains topmost even after clicking on another window.

is there any other alternative to it?? thanks in advance

Upvotes: 1

Views: 503

Answers (2)

Milos Stamenkovic
Milos Stamenkovic

Reputation: 82

You should make corrections in your method calling order. Try the following:

if (!ApplicationContext.ContactsViewModel.IsWindowOpen)
{
    ApplicationContext.CurrentChatView.Dispatcher.Invoke(() =>
    {
        if (!Window.IsVisible)
        {
            Window.Show();
        }

        if (Window.WindowState == WindowState.Minimized)
        {
            Window.WindowState = WindowState.Normal;
        }

        Window.Activate();
        Window.Topmost = true;  // important
        Window.Topmost = false; // important
        Window.Focus();         // important
    });
}

Upvotes: 2

AnnaS
AnnaS

Reputation: 76

You can use window.Show() / window.Hide() methods to switch between visible and hidden mode:

private void ShowCurrentWindows()
{
   foreach (Window window in Application.Current.Windows)
   {
        if (!window.IsVisible)
        {
            window.Show();
        }
    }
}

private void HideCurrentWindows()
{
     foreach (Window window in Application.Current.Windows)
     {
         if (window.IsVisible)
         {
            window.Hide();
         }
     }
}

Upvotes: 0

Related Questions