This is a migrated thread and some comments may be shown as answers.

Display in app without main window

3 Answers 403 Views
NotifyIcon
This is a migrated thread and some comments may be shown as answers.
Andreas
Top achievements
Rank 1
Andreas asked on 09 Nov 2020, 07:43 AM

Hi,

Is there any way to display the new NotifyIcon in an application that doesn't have any window?

I tried putting it in app.xaml like this:

<ResourceDictionary>
            <telerik:RadNotifyIcon x:Key="NotifyIcon"
                                   x:Name="NotifyIcon"         
                                   ShowTrayIcon="True"         
                                   TooltipContent="Test"         
                                   TrayIconSource="/TestNotifyIcon;component/SDK icon.ico"         
                                   GuidItem="020fea20-de2d-411f-87dd-111cd1e4f7fb">
            </telerik:RadNotifyIcon>
        </ResourceDictionary>

 

And then in App.xaml.cs:

public partial class App : Application
    {
        private RadNotifyIcon _taskbarIcon;
 
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
 
            _taskbarIcon = (RadNotifyIcon)FindResource("NotifyIcon");
        }
 
        protected override void OnExit(ExitEventArgs e)
        {
            if (_taskbarIcon != null)
                _taskbarIcon.Dispose();
 
            base.OnExit(e);
        }
    }

 

But the icon does not display...

We are currently using Hardcodet.NotifyIcon.Wpf package for this, and with that component this kind of code works, but we would like to use the Telerik component instead!

Regards
Andreas

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 11 Nov 2020, 06:36 PM

Hello Andreas,

Yes, you can use the RadNotifyIcon without a Window. To do so, you can call the AddIcon() method of the control after you get it from the Resources. For example:

protected override void OnStartup(StartupEventArgs e)
{
	base.OnStartup(e);
	_taskbarIcon = (RadNotifyIcon)FindResource("NotifyIcon");
	_taskbarIcon.AddIcon();
}

I hope this helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Andreas
Top achievements
Rank 1
answered on 11 Nov 2020, 08:40 PM

Hi,

Well that did work, the icon displays just fine, but now comes the next problem.

When I try display a context menu, the menu doesn't show at all. Did like this:

<Application x:Class="TestNotifyIcon.App"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
    <Application.Resources>
        <ResourceDictionary>
            <telerik:RadNotifyIcon x:Key="NotifyIcon"
                                   x:Name="NotifyIcon"         
                                   ShowTrayIcon="True"         
                                   TooltipContent="Test"         
                                   TrayIconSource="/TestNotifyIcon;component/SDK icon.ico"         
                                   GuidItem="020fea20-de2d-411f-87dd-111cd1e4f7fb"
                                   ContextMenuActivationMouseEvent="All">
                <telerik:RadNotifyIcon.TrayContextMenu>
                    <telerik:RadContextMenu IconColumnWidth="0">
                        <telerik:RadMenuItem Header="Item 1" />
                        <telerik:RadMenuItem Header="Item 2" />
                        <telerik:RadMenuItem Header="Item 3" />
                    </telerik:RadContextMenu>
                </telerik:RadNotifyIcon.TrayContextMenu>
            </telerik:RadNotifyIcon>
        </ResourceDictionary>
    </Application.Resources>
</Application>

 

My app doesn't have any visible window (it's just a tray app).

As you can see in my app.xaml, the StartupUri isn't set.

I guess this is why it wont work...

 

Regards
Andreas

0
Accepted
Martin Ivanov
Telerik team
answered on 13 Nov 2020, 12:47 PM

Hello Andreas,

The tray context menu requires the RadNotifyIcon to be included in the visual tree of the application (which requires a Window), or at least the hosting application to have a rendered MainWindow. To achieve your requirement you can show a hidden window. For example:

protected override void OnStartup(StartupEventArgs e)
{
	base.OnStartup(e);
	this.MainWindow = new Window() 
	{
		ShowInTaskbar = false, 
		Width = 0,
		Height = 0, 
		Visibility = Visibility.Hidden,
		ResizeMode = ResizeMode.NoResize, 
		WindowStyle = WindowStyle.None 
	};
	this.MainWindow.Show();

	_taskbarIcon = (RadNotifyIcon)FindResource("NotifyIcon");
	_taskbarIcon.AddIcon();           
}

Note that the Window needs several settings in order to get hidden from the Alt+Tab menu, the taskbar and from the screen at all.

I also attached a project showing this approach. I hope this helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
NotifyIcon
Asked by
Andreas
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Andreas
Top achievements
Rank 1
Share this question
or