WPF NotifyIcon Crash On First Run - The root Visual of a VisualTarget cannot have a parent
Asked Answered
F

3

7

Update: Issue appears to be introduced with .NET 4.5.2. Problem does not occur with either 4.5.1 or 4.5.

I have a strange issue I am having difficulty debugging. We have a WPF application built on top of the NotifyIcon made by Philipp Sumi. http://www.codeproject.com/Articles/36468/WPF-NotifyIcon http://www.hardcodet.net/wpf-notifyicon

We are using the verion from nugget:

<package id="Hardcodet.NotifyIcon.Wpf" version="1.0.5" targetFramework="net45" />

The problem is that the first time (and only the first time) the application is ran, it fails with the following exception. It is easy to recreate by hovering the mouse over the system tray icon when it appears. On subsequent runs there is no problem. The application does not have any saved state or persistent data. I'm not aware of any difference between the first and subsequent runs. It does however start much faster the second time. The same problem occurs in the windowless example application that comes with NotifyIcon.

Exception Message and Stack Trace

Frost answered 3/3, 2015 at 14:0 Comment(3)
Since you have the source code of this component, you can try to solve your problem yourself. Just start a debugging session and catch that exception in the debugger. Inspect the relevant objects. Perhaps, there is a bug in the component, so you could then post it to the author or propose a solution.Eisenstark
@Eisenstark Been working on that. The issue occurs when setting the ToolTip.IsOpen property to true. The property throws an ArgumentException. I haven't found anything wrong within the component's source code.Frost
"if (_notificationWindow.Visibility == Visibility.Visible) return;" fixed this issue for me. I am showing the window which is a child of the notify icon (_notificationWindow) immediately after this.Abrams
F
0

I have not been able to identify what the real cause of the problem is. It seems to be changes made in the 4.5.2 version of the framework. I did find the following work around.

At the beginning of the application simply create a tool tip and display it. This seems to cause things to get constructed correctly. For example:

[STAThread]
static void Main()
{
    ToolTip tt = new ToolTip();
    tt.IsOpen = true;
    tt.IsOpen = false;
...
}

I would still like to know what the real issue is, so if anyone knows please post.

Frost answered 10/3, 2015 at 17:50 Comment(2)
Did you guys ever figure this out?Fraction
@Fraction Nope. We released the application with this work around. As far as I know there haven't been any issues.Frost
T
0

I have the same problem. Unfortunately I'm not able to reproduce that behaviour but I have another program which uses this library, which works fine. So the main difference is that the working program is not only using the ToolTipText property in the XAML part but also provides a UIElement for the TrayToolTip property.

<hc:TaskbarIcon.TrayToolTip>
    <CustomUIElement/>
</hc:TaskbarIcon.TrayToolTip>

So maybe this could be an alternative workaround.

(Sorry for all the cancelled comments, just figured out that code blocks are not supported in comments)

Telegraphy answered 18/3, 2015 at 8:10 Comment(0)
P
0

My solution was to remove icon to null. Removing this solved the same problem as described in the question. Even though the line was in Window_Closing the app crached on startup.

internal System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
....
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
  ni.Icon = Null;
}

Now I hide the icon on Window_Closing using: (Otherwise the icon stays in the nitification area)

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   ni.Visible = false;
}

BTW: Using the "ToolTip-solution" added more the 10 sek startup time to my app.

Porshaport answered 22/9, 2015 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.