I was wondering how to integrate NotifyIcon using Caliburn.Micro.
I am trying to integrate with Caliburn using the low-level Caliburn API. Here are the classes:
ITrayIconManager
public interface ITrayIconManager
{
ITrayIcon GetOrCreateFor<T>();
}
ITrayIcon (wrapper around TaskbarIcon from WPF NotifyIcon)
public interface ITrayIcon : IDisposable
{
void ShowBalloonTip(string title, string message, BalloonIcon symbol);
void Show();
void Hide();
}
ISetTrayIconInstance
public interface ISetTrayIconInstance
{
ITrayIcon Icon { set; }
}
TrayIconwrapper
public class TrayIconWrapper : ITrayIcon
{
private readonly TaskbarIcon icon;
public TrayIconWrapper(TaskbarIcon icon)
{
this.icon = icon;
}
public bool IsDisposed { get; private set; }
public void Dispose()
{
icon.Dispose();
IsDisposed = true;
}
public void Show()
{
icon.Visibility = Visibility.Visible;
}
public void Hide()
{
icon.Visibility = Visibility.Collapsed;
}
public void ShowBalloonTip(string title, string message, BalloonIcon symbol)
{
icon.ShowBalloonTip(title, message, symbol);
}
}
TrayIconmanager
public class TrayIconManager : ITrayIconManager
{
private readonly IDictionary<WeakReference, WeakReference> icons;
public TrayIconManager()
{
icons = new Dictionary<WeakReference, WeakReference>();
}
public ITrayIcon GetOrCreateFor<T>()
{
if (!icons.Any(i => i.Key.IsAlive && typeof(T).IsAssignableFrom(i.Key.Target.GetType())))
return Create<T>();
var reference = icons.First(i => i.Key.IsAlive && typeof(T).IsAssignableFrom(i.Key.Target.GetType())).Value;
if (!reference.IsAlive)
return Create<T>();
var wrapper = (TrayIconWrapper)reference.Target;
if (wrapper.IsDisposed)
return Create<T>();
return wrapper;
}
private ITrayIcon Create<T>()
{
var rootModel = IoC.Get<T>();
var view = ViewLocator.LocateForModel(rootModel, null, null);
var icon = view is TaskbarIcon ? (TaskbarIcon)view : new TaskbarIcon();
var wrapper = new TrayIconWrapper(icon);
ViewModelBinder.Bind(rootModel, view, null);
SetIconInstance(rootModel, wrapper);
icons.Add(new WeakReference(rootModel), new WeakReference(wrapper));
return wrapper;
}
private void SetIconInstance(object rootModel, ITrayIcon icon)
{
var instance = rootModel as ISetTrayIconInstance;
if (instance != null)
instance.Icon = icon;
}
}
This is the code, now how to use it? This code is based on Caliburn View - ViewModel binding, i.e. I need to create a ViewModel for TasbarkIcon and View (which should be inherited from the TaskbarIcon element ):
TrayIconViewModel
public class TrayIconViewModel : IMainTrayIcon, ISetTrayIconInstance
{
public TrayIconViewModel()
{
}
public ITrayIcon Icon { get; set; }
public void ShowWindow()
{
Icon.Hide();
System.Windows.Application.Current.MainWindow.Show();
}
ITrayIcon is a shell for managing TaskbarIcon. Now I can call methods on it from my ViewModel, which is great.
TrayIconView (cal: Message: Attach - ShowWindow )
<tb:TaskbarIcon x:Class="Communicator.Softphone.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="http://www.hardcodet.net/taskbar"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
IconSource="/Communicator.ControlLibrary;component/Assets/phone_icon.ico"
ToolTipText="Secretária do Futuro - Comunicador"
Visibility="Collapsed"
cal:Message.Attach="[Event TrayLeftMouseDown] = [Action ShowWindow()]">
ShellViewModel (trayIcon - TaskbarIcon):
private ITrayIcon trayIcon;
protected override void OnActivate()
{
trayIcon = trayIconManager.GetOrCreateFor<IMainTrayIcon>();
ActivateItem(containers.FirstOfType<IPhone>());
}
public override void CanClose(Action<bool> callback)
{
trayIcon.Show();
trayIcon.ShowBalloonTip("Comunicador", "Comunicador foi minimizado", BalloonIcon.Info);
(GetView() as Window).Hide();
callback(false);
}
trayIcon.Show() , trayIcon.ShowBallonTip(...) , , .
:
- Binding Message.Attach , Caliburn , .
ShowBallonTip , , , TaskbarIcon. ( )