pds
pds

Reputation: 1

How can use WPF NotifyIcon

I try to use WPF Notify icon http://www.codeproject.com/Articles/36468/WPF-NotifyIcon. I create a new wpf project, I import dll from the sample project and then I copy the xaml part and code. XAML:

<Window x:Class="trayicon.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="MainWindow" Height="350" Width="525">
<Grid>

<!--
  in order to create a NotifyIcon, all you need is the
  namespace declaration (see above on line 4) and a simple
  declaration
-->
    <tb:TaskbarIcon
  IconSource="Error.ico"
  ToolTipText="hello world"
    />
</Grid>
</Window>

C#code:

using Hardcodet.Wpf.TaskbarNotification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace trayicon
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //Note: XAML is suggested for all but the simplest scenarios
            TaskbarIcon tbi = new TaskbarIcon();
            tbi.Icon = Resources.Error;
            tbi.ToolTipText = "hello world";
        }
    }
}

There is an error in line tbi.Icon = Resources.Error; and if I comment that line I obtained a XamlParseException in line ToolTipText="hello world"

Can you help? Thanks

Upvotes: 0

Views: 1471

Answers (1)

JobaDiniz
JobaDiniz

Reputation: 1023

First of all, you are instanciating 2 notfiyicons. One in XAML and one in code behind. There's no need for that. Leave only the XAML one.

The error about the IconSource is that VS couldn't find "Error.ico". You have to add it to the project and set the compile to "Resource" (right click on the Error.ico, go to properties and change there).

As for the second error (when you commented out): you can't comment out properties of controls in XAML, because it will throw parse errros.

Upvotes: 1

Related Questions