MainWindow已加载但始终不可见

时间:2012-04-29 10:36:25

标签: wpf clipboard visibility tray

我想构建一个WPF应用程序,启动时只有一个托盘图标。如果用户与托盘的上下文菜单中的菜单项进行交互,则会有Windows。

我需要加载MainWindow,所以我可以收听Clipboard Changed Events。但我不想表现出来。

我试过了:

<Window x:Class="ClipboardListener.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tb="http://www.hardcodet.net/taskbar"
    Title="Should not see me"
    ShowInTaskbar="False" Visibility="Collapsed" Opacity="100"
    Width="320" Height="240">

但它仍然显示出来?将Visibility设置为Hidden对我来说不起作用,因为我需要一些Window来使用WinAPI注册剪贴板事件监听器。

任何想法?

1 个答案:

答案 0 :(得分:2)

我最近的任务非常相似。所有我试图使窗口隐形,我的谷歌搜索,我的堆栈溢出等失败。最后我感觉隐形窗口是某种原因不应该出现在WPF中的东西。如果像WinForms中那样有TrayIcon控件,那将是一件容易的事。不幸的是,WPF没有TrayIcon。这导致了WinForms中存在的那个。

关于这个问题

Here's a good article。我的代码使用了这个dll:

<Window x:Class="ScannerClientWpf.TrayIcon"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ni="clr-namespace:Hardcodet.Wpf.TaskbarNotification;assembly=Hardcodet.Wpf.TaskbarNotification"
    Title="TrayIcon" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ShowActivated="False" ShowInTaskbar="False" >
<Grid>
    <ni:TaskbarIcon IconSource="/ScannerClient;component/app.ico" 
                    ToolTipText="ScannerClient">
        <ni:TaskbarIcon.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Close" Click="MenuItem_Click"/>
            </ContextMenu>
        </ni:TaskbarIcon.ContextMenu>
    </ni:TaskbarIcon>
</Grid>

相关问题