Jon
Jon

Reputation: 385

WPF-NotifyIcon not working when moved into an XAML dictionary

I'm using WPF-NotifyIcon, and am following this tutorial, and it works when I put the XAML code in my MainWindow.xaml, however once I move onto the "Creating the NotifyIcon from a Resource Dictionary" part it stops showing up in the tray.

I have a Dictionary1.xaml with the following code in it:

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

    <!-- Globally declared notify icon -->
    <tb:TaskbarIcon x:Key="MyNotifyIcon"
            IconSource="/Notifier;component/assets/icon_16x.ico"
            ToolTipText="Notifier" MenuActivation="RightClick" Visibility="Visible" />

</ResourceDictionary>

An App.xaml with the following code:

<Application x:Class="Notifier.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary Source="Dictionary1.xaml" />
    </Application.Resources>
</Application>

and I just copied over the App class code from that section of the tutorial.

What am I doing wrong? Why isn't the icon showing up? And on a related note, how can I have a program that doesn't have a MainWindow, but instead just runs from the taskbar (Which would be ideal with this control).

Edit:

I took out the StartupUri from App.xaml and put the following code in my App.xaml.cs:

    protected override void OnStartup(StartupEventArgs e) {
        base.OnStartup(e);
        tb = (TaskbarIcon)FindResource("MyNotifyIcon");
        tb.Visibility = Visibility.Visible;
        //new MyClassIWantToInstantiate();
    }

It works perfectly, but is there anything wrong with doing this?

Upvotes: 1

Views: 1078

Answers (2)

jfrobishow
jfrobishow

Reputation: 2895

Ran into the same when I was following the tutorial on https://www.codeproject.com/Articles/36468/WPF-NotifyIcon-2#hello

The text indicates that you should instantiate your taskbarIcon within Initapplication() which did not work for me.

From the tutorial:

private void InitApplication()
{
  //initialize NotifyIcon
  tb = (TaskbarIcon) FindResource("MyNotifyIcon");
}

However, when looking at the included Samples it's done in the OnStartup override which seems to make more sense to me.

The following works as expected.

private TaskbarIcon tb;

protected override void OnStartup(StartupEventArgs e)
{
  base.OnStartup(e);
  //create the notifyicon (it's a resource declared in NotifyIconResources.xaml
  tb = (TaskbarIcon)FindResource("MyNotifyIcon");
}

Upvotes: 0

sa_ddam213
sa_ddam213

Reputation: 43596

You should be adding that ResourceDictionary to the MergedDictionaries section if you want to use the resources normally.

Example:

<Application x:Class="Notifier.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Upvotes: 2

Related Questions