diff --git a/README.adoc b/README.adoc index c39de75..2a97abf 100644 --- a/README.adoc +++ b/README.adoc @@ -91,6 +91,18 @@ Each sample is tagged with it's difficulty. The degree of difficulty describes h |=== +=== DesktopIntegration-Samples + +[cols="25h,25,50"] +|=== +| Sample | Difficulty | Buzz-Words + +| link:src/Avalonia.Samples/Desktop/TrayIcon[Tray Icon Sample] +| 🐣 Beginner +| TrayIcon, ReactiveUI, Binding + +|=== + === ✒️ Drawing-Samples [cols="25h,25,50"] diff --git a/src/Avalonia.Samples/Avalonia.Samples.sln b/src/Avalonia.Samples/Avalonia.Samples.sln index 636e556..c78a4b4 100644 --- a/src/Avalonia.Samples/Avalonia.Samples.sln +++ b/src/Avalonia.Samples/Avalonia.Samples.sln @@ -59,6 +59,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleToDoList", "CompleteA EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RectPainter", "Drawing\RectPainter\RectPainter.csproj", "{2B746401-384F-484A-810E-7A65288165E0}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrayIcon", "DesktopIntegration\TrayIcon\TrayIcon.csproj", "{EB8D1498-2528-4FE5-B2B0-515293A8880C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -137,6 +139,10 @@ Global {2B746401-384F-484A-810E-7A65288165E0}.Debug|Any CPU.Build.0 = Debug|Any CPU {2B746401-384F-484A-810E-7A65288165E0}.Release|Any CPU.ActiveCfg = Release|Any CPU {2B746401-384F-484A-810E-7A65288165E0}.Release|Any CPU.Build.0 = Release|Any CPU + {EB8D1498-2528-4FE5-B2B0-515293A8880C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EB8D1498-2528-4FE5-B2B0-515293A8880C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EB8D1498-2528-4FE5-B2B0-515293A8880C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EB8D1498-2528-4FE5-B2B0-515293A8880C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Avalonia.Samples/DesktopIntegration/TrayIcon/AboutWindow.axaml b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/AboutWindow.axaml new file mode 100644 index 0000000..acd6896 --- /dev/null +++ b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/AboutWindow.axaml @@ -0,0 +1,18 @@ + + + + + + + + + + + + diff --git a/src/Avalonia.Samples/DesktopIntegration/TrayIcon/AboutWindow.axaml.cs b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/AboutWindow.axaml.cs new file mode 100644 index 0000000..766e3b3 --- /dev/null +++ b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/AboutWindow.axaml.cs @@ -0,0 +1,18 @@ +using Avalonia.Controls; +using Avalonia.Interactivity; + +namespace TrayIcon +{ + public partial class AboutWindow : Window + { + public AboutWindow() + { + InitializeComponent(); + } + + void cmdClose_Click(object? sender, RoutedEventArgs e) + { + this.Close(); + } + } +} diff --git a/src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.axaml b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.axaml new file mode 100644 index 0000000..148ed02 --- /dev/null +++ b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.axaml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.axaml.cs b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.axaml.cs new file mode 100644 index 0000000..1288781 --- /dev/null +++ b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.axaml.cs @@ -0,0 +1,56 @@ +using System.Windows.Input; + +using Avalonia; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Markup.Xaml; + +using ReactiveUI; + +namespace TrayIcon +{ + public partial class App : Application + { + public override void Initialize() + { + AvaloniaXamlLoader.Load(this); + } + + public App() + { + AboutCommand = ReactiveCommand.Create(ShowAboutWindow); + ExitCommand = ReactiveCommand.Create(ExitApplication); + + DataContext = this; + } + + public override void OnFrameworkInitializationCompleted() + { + if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + { + _lifetime = desktop; + + desktop.ShutdownMode = Avalonia.Controls.ShutdownMode.OnExplicitShutdown; + } + + base.OnFrameworkInitializationCompleted(); + } + + IClassicDesktopStyleApplicationLifetime? _lifetime; + + public ICommand AboutCommand { get; } + public ICommand ExitCommand { get; } + + void ShowAboutWindow() + { + var window = new AboutWindow(); + + window.Show(); + } + + void ExitApplication() + { + _lifetime?.Shutdown(); + } + } +} + diff --git a/src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.manifest b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.manifest new file mode 100644 index 0000000..bedffa8 --- /dev/null +++ b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.manifest @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/src/Avalonia.Samples/DesktopIntegration/TrayIcon/AssemblyInfo.cs b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/AssemblyInfo.cs new file mode 100644 index 0000000..d09de88 --- /dev/null +++ b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/AssemblyInfo.cs @@ -0,0 +1 @@ +using System.Runtime.CompilerServices; diff --git a/src/Avalonia.Samples/DesktopIntegration/TrayIcon/Icon.ico b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/Icon.ico new file mode 100644 index 0000000..1c290f2 Binary files /dev/null and b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/Icon.ico differ diff --git a/src/Avalonia.Samples/DesktopIntegration/TrayIcon/Program.cs b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/Program.cs new file mode 100644 index 0000000..91ec561 --- /dev/null +++ b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/Program.cs @@ -0,0 +1,25 @@ +using System; + +using Avalonia; + +namespace TrayIcon +{ + class Program + { + [STAThread] + public static void Main(string[] args) + { + var app = BuildAvaloniaApp(); + + app.StartWithClassicDesktopLifetime(args); + } + + public static AppBuilder BuildAvaloniaApp() + { + return AppBuilder.Configure() + .UsePlatformDetect() + .WithInterFont() + .LogToTrace(); + } + } +} diff --git a/src/Avalonia.Samples/DesktopIntegration/TrayIcon/README.md b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/README.md new file mode 100644 index 0000000..ea74aaa --- /dev/null +++ b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/README.md @@ -0,0 +1,7 @@ +# TrayIcon + +Sample application that shows a tray icon. + +* The tray icon must have an associated icon. The icon should be included in the project as an ``. +* This sample sets the `DataContext` for `App` to `this`, which allows the command to be defined in the same class and then bound in the `.axaml` file. Consider using a separate view model in production code. + diff --git a/src/Avalonia.Samples/DesktopIntegration/TrayIcon/TrayIcon.csproj b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/TrayIcon.csproj new file mode 100644 index 0000000..e01c5f0 --- /dev/null +++ b/src/Avalonia.Samples/DesktopIntegration/TrayIcon/TrayIcon.csproj @@ -0,0 +1,21 @@ + + + WinExe + net8.0 + enable + true + + + + + + + + + + + + + + +