Provides high-level APIs to create task executing tray applications
$ dotnet add package TrayAppUtilityProvides high-level APIs to create task-executing tray applications in Window Presentation Foundation (WPF)
In order to start using the tray app utility we need create a new project from WPF application template and set its window as the startup window. We can do this in App.xaml file
<Application x:Class="Tutorial.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="pack://application:,,,/TrayAppUtility;component/TrayApp.xaml"> <!-- Set tray app as startup window-->
</Application>
Without any further actions, the application should not generate an empty tray app with a context menu containing default utility actions.
Using the tray app utility is quite simple. One of the first things we might want to do is set our custom icon. This can be done by adding a png image with the name "TrayIcon.png" to the project files.
Then you need to make the PNG file build action to be set to "Embedded resource".
This will make your custom icon discoverable by tray app utility. Once the project is built, you should see tray app use your icon.
In order to start adding your own actions to the tray app, declare a public static method. The name of the method will be used as the name of the action. Here is an example of the full method signature:
[TrayAction]
public static void Action(CancellationTokenSource cancel)
{
}
Of course, there is no point in leaving an empty action so let's fill it with some work to do:
[TrayAction]
public static void Action(CancellationTokenSource cancel)
{
var length = 100;
Progress.Total = length;
for (int i = 0; i < length; i++)
{
if (cancel.IsCancellationRequested)
{
Log.Write($"Cancelling Default Action");
return;
}
Thread.Sleep(100);
Progress.Increment($"Processed item {i}");
}
}
This is a fully implemented tray action. That's how it should look in action:
Default actions are created identically as tray actions but using a different attribute. Here is an example of making a default action:
[TrayDefault]
public static void DefaultAction(CancellationTokenSource cancel)
{
...
}
Now tray app will execute this action upon double click.
Some actions are so simple that they don't need logs. You can disable logging for a particular action using NoLog attribute:
[TrayAction]
[TrayDefault]
[NoLog]
public static void DefaultAction(CancellationTokenSource cancel)
{
...
}
You will still see log messages in tray tooltips but they won't be saved into a dedicated log file.
We may want to execute actions automatically at a specified interval. This can be achieved using Autorun attribute. The attribute accepts a TimeSpan string in format Hours:Minutes:Seconds.Milliseconds. Here is how it looks in use:
[TrayAction]
[Autorun("00:00:30")]
public static void Notification(CancellationTokenSource cancel)
{
TrayUtils.ShowNotification("Notification", "Message text",
() => Log.Write("Log entry from notification"));
}
Now on startup and then every 30 seconds you'll see this toast notification: