单击“退出”后,在任务栏图标中隐藏上下文菜单

时间:2019-06-06 12:14:12

标签: c# wpf

当我单击上下文菜单中的退出按钮时,它仍然会出现,直到方法完成。

<tb:TaskbarIcon Name="TrayIcon" IconSource="Icon.ico" ToolTipText="text" Visibility="Visible"  MenuActivation="LeftOrRightClick">
            <tb:TaskbarIcon.ContextMenu>
                <ContextMenu Name="ContextMenu">
                    <ContextMenu.Style>
                        <Style TargetType="{x:Type ContextMenu}">
                            <Setter Property="ItemsPanel" Value="{StaticResource MenuTPL}"/>
                        </Style>
                    </ContextMenu.Style>
                    <MenuItem x:Name="LogIn" Click="LogIn_Click" Header="points"></MenuItem>
                    <MenuItem x:Name="Exit" Click="Exit_Click" Header="exit"></MenuItem>
                </ContextMenu>
            </tb:TaskbarIcon.ContextMenu>
        </tb:TaskbarIcon>

C#代码为:

private void Exit_Click(object sender, RoutedEventArgs e)
    {
        TrayIcon.Visibility = Visibility.Hidden;  
        TrayIcon.ContextMenu.Visibility = Visibility.Hidden;         
        Application.Current.Shutdown();
    }

我无法将shutdown放在背景中。有什么办法解决吗?

1 个答案:

答案 0 :(得分:0)

您必须离开事件处理程序。为此使用Dispatcher.BeginInvoke

Dispatcher.BeginInvoke((Action)(()=> { Application.Current.Shutdown(); }));