Вопрос

I want to build a WPF Application that, when started, only has a tray Icon. If the User interacts with Menu Entries from the Tray's Context Menu, there will be Windows.

I need to load the MainWindow though, so I can listen to Clipboard Changed Events. But I don't want to show it.

I tried:

<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">

But it still shows up? Setting Visibility to Hidden does not work for me since I need some Window to register the Clipboard Event Listener with the WinAPI.

Any Ideas?

Это было полезно?

Решение

I've recently had very similar task. All my attempts to make Window invisible, my googling, my stackoverflowing etc. failed. Finally I had a feeling that invisible window is something that should not be in WPF on some reason. It would be an easy task if there was TrayIcon control like in WinForms. Unfortunately, WPF does not have TrayIcon. This leads to the one that is present in WinForms.

Here's a good article on the issue. And my code that uses this 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>

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top