0

I'm using NotifyIcon to implement a "minimize icon to tray" feature in my WPF app. The minimize works just fine, however if I click "open", the icon will re-appear on the taskbar but the app doesn't re-appear. I'm setting the TopMost property to true (I've tried the two solutions below) but still no luck. Very new to desktop apps so any direction/theories much appreciated.

Code Behind

    private void TrayIconShowWindow_Click(object sender, RoutedEventArgs e)
    {
        WindowState = System.Windows.WindowState.Normal;
        Visibility = Visibility.Visible;

        // Neither of these work
        Application.Current.MainWindow.Topmost = true;
        Topmost = true;
    }

XAML

<taskbar:TaskbarIcon.ContextMenu>
    <ContextMenu>
        <MenuItem Header="Open Window" Click="TrayIconShowWindow_Click">
            <MenuItem.Icon>
                <Image Width="16" Height="16" Source="Assets/OpenScreen.png"/>
            </MenuItem.Icon>
        </MenuItem>
        <Separator/>
        <MenuItem Header="Exit" Click="TrayIconExitApp_Click">
            <MenuItem.Icon>
                <Image Width="16" Height="16" Source="Assets/Close.png"/>
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu>
</taskbar:TaskbarIcon.ContextMenu>

NealR
  • 10,189
  • 61
  • 159
  • 299
  • have you tried the first 2 againstApplication.Current.MainWindow ? as currently you are appling to the TaskbarIcon – MikeT Apr 01 '16 at 16:05
  • How so? The `TrayIconShowWindow_Click` is in my main window class. – NealR Apr 01 '16 at 16:09
  • 1
    Have you tried `myWindow.Activate();` `Topmost` is a setting so that the window is always the topmost but it doesn't display the window if minimized – Lithium Apr 01 '16 at 16:14
  • @NealR If you select the item in the taskbar does it display? – CathalMF Apr 01 '16 at 16:18
  • @CathalMF yes if I click the taskbar icon, when it reappears, the window will come up as expected. – NealR Apr 01 '16 at 17:12

2 Answers2

1

Use the Activate method on the Window

private void TrayIconShowWindow_Click(object sender, RoutedEventArgs e)
{
    this.Activate();
}
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
0
Application.Current.MainWindow.WindowState = WindowState.Maximized;
CathalMF
  • 9,705
  • 6
  • 70
  • 106