Can't get attached Message firing

Feb 12, 2011 at 1:39 PM

Hi,

Maybe i am stupid, but i can't get an Action Message working in one specific case. Probably it has something to do with the particular control i am trying to attach to.

I am using the great WPF trayicon library from Philipp Sumi (http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx).

I try to show a view on double click on the tray icon. The tray view control (TaskbarIcon) is derived from FrameworkElement and supports a couple of routed events, one of them TrayMouseDoubleClick.

The tray icon is the root shell of the application, so i have overriden the standard Caliburn WindowManager to create a TrayIconViewModel for the root model. This works fine. The corresponding TrayIconView looks like this:

<UserControl x:Class="ServerGui.Views.TrayIconView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:tb="clr-namespace:Hardcodet.Wpf.TaskbarNotification;assembly=Hardcodet.Wpf.TaskbarNotification"
xmlns:Views="clr-namespace:ServerGui.Views"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
mc:Ignorable="d">
<tb:TaskbarIcon ToolTipText="Reg.Net Server running on XP/2003"
PopupActivation="LeftClick"
MenuActivation="RightClick"
cal:Message.Attach="[Event TrayMouseDoubleClick] = [Action ShowAbout()]"
IconSource="{Binding TrayIcon}">
<tb:TaskbarIcon.TrayToolTip>
<Views:TrayBalloonView />
</tb:TaskbarIcon.TrayToolTip>
<tb:TaskbarIcon.TrayPopup>
<Views:TrayPopupView />
</tb:TaskbarIcon.TrayPopup>
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem Header="Show Console"
cal:Message.Attach="ShowConsole()" />
<MenuItem Header="About Reg.Net Server"
cal:Message.Attach="ShowAbout()" />
<Separator />
<MenuItem Header="Exit"
cal:Message.Attach="ExitProgram()" />
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>
</UserControl>

The Context Menu actions work fine. The double click action on the TaskbarIcon itself not.

I checked that the RoutedEvent "TrayMouseDoubleClick" is fired correctly by adding some code to the code-behind of TrayIconView:

 

        public TrayIconView()
{
InitializeComponent();
foreach (RoutedEvent routedEvent in EventManager.GetRoutedEvents())
{
EventManager.RegisterClassHandler(GetType(),
routedEvent,
new RoutedEventHandler(AllEventsLogger));
}
}


static void AllEventsLogger(object sender, RoutedEventArgs routedEventArgs)
{
Console.WriteLine(routedEventArgs.RoutedEvent.ToString());
}

 

Here is the output in the debugger console window when i double-click the tray icon:

 

TaskbarIcon.TrayMouseMove
TaskbarIcon.TrayMouseMove
TaskbarIcon.TrayMouseMove
TaskbarIcon.TrayLeftMouseDown
TaskbarIcon.TrayMouseMove
TaskbarIcon.TrayLeftMouseUp
TaskbarIcon.TrayMouseMove
TaskbarIcon.TrayMouseDoubleClick
TaskbarIcon.TrayMouseMove
TaskbarIcon.TrayMouseMove

 

I can't figure out why it doesn't work. Any help on this is really appreciated.

 

Regards,

    Gunter

Feb 12, 2011 at 1:39 PM

By the way, i have had a hard time trying to figure out why it doesn't work.

Inside the Caliburn Micro code everything seems to be ok, i stepped through Message.OnAttachChanged. I also enabled all WPF trace settings.

But there is no hint at all why the EventTrigger isn't get triggered by TrayMouseDoubleClick.

Does anyone have some hints how to debug this?

 

Regards,

    Gunter

Coordinator
Feb 12, 2011 at 2:23 PM

Check to make sure that TrayMouseDoubleClick is actually an EventHandler and not something like Action<T>. The EventTrigger only seams to work with events that follow the standard pattern. If that turns out to be the problem, you would need to define a custom trigger or contact the control author and ask him to redefine the event following the standard pattern. This is just a guess, but I saw something like this happen once before with the EventTrigger. To tell for sure if it is happening in the trigger, you can put break points in the ActionMessage OnAttach and Invoke methods. If OnAttach is called, but Invoke is not called, then it'sprobably an issue with the trigger itself.

Feb 12, 2011 at 2:45 PM

Here is the definition:

    /// <summary>
    /// TrayMouseDoubleClick Routed Event
    /// </summary>
    public static readonly RoutedEvent TrayMouseDoubleClickEvent = EventManager.RegisterRoutedEvent("TrayMouseDoubleClick",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));

    /// <summary>
    /// Occurs when the user double-clicks the taskbar icon.
    /// </summary>
    public event RoutedEventHandler TrayMouseDoubleClick
    {
      add { AddHandler(TrayMouseDoubleClickEvent, value); }
      remove { RemoveHandler(TrayMouseDoubleClickEvent, value); }
    }

So TrayMouseDoubleClick is an RoutedEventHandler. That seems to be pretty much the standard implementation of a routed event handler.

OnAttached() gets called for the double click action, MethodName and AssociatedObject looks good.

But Invoke() doesn't get called.

I traced through OnAttached and noticed a possible problem. IsLoaded was false on the view so an event handler was attached (AssociatedObject.Loaded += ElementLoaded).

ElementLoaded though never got called. That is probably a problem of the implementation of TaskbarIcon. I have to look for a workaround it seems.

Thanks for the hints, they pointed me to the right spot :).

 

Regards,

      Gunter

Feb 12, 2011 at 3:24 PM
Edited Feb 13, 2011 at 8:33 AM

I modified the TaskbarIcon class a bit and added:

 

          Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(
() => RaiseEvent(new RoutedEventArgs(LoadedEvent))));

 

just after the tray icon was created in the constructor

 

ElementLoaded gets called now, but still no Invoke()

Feb 13, 2011 at 8:32 AM

I couldn't get it to work so i now just rely on the good old (ugly :)) ICommand interface of TaskbarIcon which it also provides.

Regards,

    Gunter

Mar 9, 2011 at 5:03 PM

Can you, please, post your sample? Thanks

Mar 9, 2011 at 5:53 PM

Which sample? Do you use TaskbarIcon? My problem is a TaskbarIcon specific problem.

Mar 31, 2011 at 9:08 PM

FYI: just posted an article with the solution - http://igorshare.wordpress.com/2011/03/31/tip-showing-a-main-view-as-a-tray-app-with-caliburn-micro/