문제

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