TrayAppUtility 1.3.1
dotnet add package TrayAppUtility --version 1.3.1
NuGet\Install-Package TrayAppUtility -Version 1.3.1
<PackageReference Include="TrayAppUtility" Version="1.3.1" />
paket add TrayAppUtility --version 1.3.1
#r "nuget: TrayAppUtility, 1.3.1"
// Install TrayAppUtility as a Cake Addin #addin nuget:?package=TrayAppUtility&version=1.3.1 // Install TrayAppUtility as a Cake Tool #tool nuget:?package=TrayAppUtility&version=1.3.1
TrayAppUtility
Provides high-level APIs to create task-executing tray applications in Window Presentation Foundation (WPF)
Features
- Progress API
- Ability to set total and processed items
- Getter for estimated time to completion
- Logging API
- Each task generates a separate log file
- All tasks are automatically measured for time. Runtime is appended to the log file.
- Log files older than a month are automatically deleted
- Time-intensive action parts can be measured manually
- An attribute to disable logging for particular action
- Action
- Declared as a public static method
- Ability to set tray action that will be used to populate tray icon context menu
- Ability to set default tray action that will be executed on double click if the tray is not in the error state
- Receives an instance of log writer
- Can be canceled at any time by the user
- Ability to periodically execute action automatically
- Tray
- Custom tray icon
- Context menu populated with users' actions
- Double-clicking executes default action if specified
- When an action is being executed that uses Progress API, a radial progress bar is overlayed over the tray icon
- When an action is being executed a tooltip is set containing
- Action name
- Processed and total item counts as well as percentage completed
- Estimated time to completion
- Last log entry written
- When an action throws an unhandled exception tray app enters an error state
- Progress bar overlay changes color to red
- Double-clicking the tray icon opens the last log file and clears the error state
- Any context menu action clears the error state and starts their respective action
- Utilities
- Nice name method
- Open file or folder with default application (can wait for close)
- Method to show toast notifications with click action
- Method to schedule a task to run after currently running task is finished
Tutorial
Configuring tray app utility
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">
</Application>
Without any further actions, the application should not generate an empty tray app with a context menu containing default utility actions.
Changing tray icon
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.
Defining an action
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:
Defining a default 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.
No logging
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.
Automatic action execution
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:
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0-windows7.0 is compatible. net7.0-windows was computed. net8.0-windows was computed. |
-
net6.0-windows7.0
- Hardcodet.NotifyIcon.Wpf (>= 1.1.0)
- System.Drawing.Common (>= 8.0.0-rc.1.23419.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.