Skip to content

Commit 8e782e8

Browse files
committed
Add Avalonia example project
1 parent 088b16a commit 8e782e8

10 files changed

+246
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Avalonia;
2+
using Avalonia.Controls;
3+
using Avalonia.Platform;
4+
using DesktopNotifications.FreeDesktop;
5+
using DesktopNotifications.Windows;
6+
7+
namespace DesktopNotifications.Avalonia
8+
{
9+
/// <summary>
10+
/// Extensions for <see cref="AppBuilderBase{TAppBuilder}"/>
11+
/// </summary>
12+
public static class AppBuilderExtensions
13+
{
14+
/// <summary>
15+
/// Setups the <see cref="INotificationManager"/> for the current platform and
16+
/// binds it to the service locator (<see cref="AvaloniaLocator"/>).
17+
/// </summary>
18+
/// <typeparam name="TAppBuilder"></typeparam>
19+
/// <param name="builder"></param>
20+
/// <returns></returns>
21+
public static TAppBuilder SetupDesktopNotifications<TAppBuilder>(this TAppBuilder builder)
22+
where TAppBuilder : AppBuilderBase<TAppBuilder>, new()
23+
{
24+
return builder.AfterSetup(async b =>
25+
{
26+
var runtimeInfo = b.RuntimePlatform.GetRuntimeInfo();
27+
INotificationManager manager;
28+
29+
switch (runtimeInfo.OperatingSystem)
30+
{
31+
case OperatingSystemType.WinNT:
32+
{
33+
var context = WindowsApplicationContext.FromCurrentProcess(b.Instance.Name);
34+
manager = new WindowsNotificationManager(context);
35+
break;
36+
}
37+
38+
case OperatingSystemType.Linux:
39+
{
40+
var context = FreeDesktopApplicationContext.FromCurrentProcess();
41+
manager = new FreeDesktopNotificationManager(context);
42+
break;
43+
}
44+
45+
//TODO: OSX once implemented/stable
46+
default: return;
47+
}
48+
49+
await manager.Initialize();
50+
51+
AvaloniaLocator.CurrentMutable.Bind<INotificationManager>().ToConstant(manager);
52+
});
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Avalonia" Version="0.10.0" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\DesktopNotifications.FreeDesktop\DesktopNotifications.FreeDesktop.csproj" />
13+
<ProjectReference Include="..\DesktopNotifications.Windows\DesktopNotifications.Windows.csproj" />
14+
<ProjectReference Include="..\DesktopNotifications\DesktopNotifications.csproj" />
15+
</ItemGroup>
16+
17+
</Project>

DesktopNotifications.sln

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example", "Example\Example.
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopNotifications.FreeDesktop", "DesktopNotifications.FreeDesktop\DesktopNotifications.FreeDesktop.csproj", "{96F637EF-FD11-41F5-8F40-4E37313697E3}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesktopNotifications.Apple", "DesktopNotifications.Apple\DesktopNotifications.Apple.csproj", "{B4A6C639-79C4-48EF-955F-273CF01D7770}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopNotifications.Apple", "DesktopNotifications.Apple\DesktopNotifications.Apple.csproj", "{B4A6C639-79C4-48EF-955F-273CF01D7770}"
15+
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopNotifications.Avalonia", "DesktopNotifications.Avalonia\DesktopNotifications.Avalonia.csproj", "{5D9904D2-E102-409D-AAFE-394A3DED40D9}"
17+
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.Avalonia", "Example.Avalonia\Example.Avalonia.csproj", "{4408FE39-C5AF-453D-B6EE-E1A42504264A}"
1519
EndProject
1620
Global
1721
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -39,6 +43,14 @@ Global
3943
{B4A6C639-79C4-48EF-955F-273CF01D7770}.Debug|Any CPU.Build.0 = Debug|Any CPU
4044
{B4A6C639-79C4-48EF-955F-273CF01D7770}.Release|Any CPU.ActiveCfg = Release|Any CPU
4145
{B4A6C639-79C4-48EF-955F-273CF01D7770}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{5D9904D2-E102-409D-AAFE-394A3DED40D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{5D9904D2-E102-409D-AAFE-394A3DED40D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{5D9904D2-E102-409D-AAFE-394A3DED40D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{5D9904D2-E102-409D-AAFE-394A3DED40D9}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{4408FE39-C5AF-453D-B6EE-E1A42504264A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{4408FE39-C5AF-453D-B6EE-E1A42504264A}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{4408FE39-C5AF-453D-B6EE-E1A42504264A}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{4408FE39-C5AF-453D-B6EE-E1A42504264A}.Release|Any CPU.Build.0 = Release|Any CPU
4254
EndGlobalSection
4355
GlobalSection(SolutionProperties) = preSolution
4456
HideSolutionNode = FALSE

Example.Avalonia/App.axaml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Application xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
x:Class="Example.Avalonia.App">
4+
<Application.Styles>
5+
<FluentTheme Mode="Light"/>
6+
</Application.Styles>
7+
</Application>

Example.Avalonia/App.axaml.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Avalonia;
2+
using Avalonia.Controls.ApplicationLifetimes;
3+
using Avalonia.Markup.Xaml;
4+
5+
namespace Example.Avalonia
6+
{
7+
public class App : Application
8+
{
9+
public override void Initialize()
10+
{
11+
AvaloniaXamlLoader.Load(this);
12+
}
13+
14+
public override void OnFrameworkInitializationCompleted()
15+
{
16+
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
17+
{
18+
desktop.MainWindow = new MainWindow();
19+
}
20+
21+
base.OnFrameworkInitializationCompleted();
22+
}
23+
}
24+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>WinExe</OutputType>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Avalonia" Version="0.10.0" />
9+
<PackageReference Include="Avalonia.Desktop" Version="0.10.0" />
10+
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.0" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<ProjectReference Include="..\DesktopNotifications.Avalonia\DesktopNotifications.Avalonia.csproj" />
14+
</ItemGroup>
15+
</Project>

Example.Avalonia/MainWindow.axaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Window xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="400"
6+
Width="400" Height="400"
7+
x:Class="Example.Avalonia.MainWindow"
8+
Title="Example.Avalonia">
9+
10+
<StackPanel Margin="20" Spacing="10">
11+
<TextBox Name="TitleTextBox" Watermark="Title" UseFloatingWatermark="True" />
12+
<TextBox Name="BodyTextBox" Watermark="Body" UseFloatingWatermark="True" />
13+
14+
<Button Click="Button_OnClick" Content="Show Notification" />
15+
16+
<TextBlock Foreground="Gray">Events:</TextBlock>
17+
<ListBox Name="EventsListBox" Height="200" />
18+
</StackPanel>
19+
20+
</Window>

Example.Avalonia/MainWindow.axaml.cs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
3+
using System.Diagnostics;
4+
using Avalonia;
5+
using Avalonia.Controls;
6+
using Avalonia.Interactivity;
7+
using Avalonia.Markup.Xaml;
8+
using DesktopNotifications;
9+
10+
namespace Example.Avalonia
11+
{
12+
public class MainWindow : Window
13+
{
14+
private readonly TextBox _bodyTextBox;
15+
private readonly ListBox _eventsListBox;
16+
private readonly TextBox _titleTextBox;
17+
private readonly INotificationManager _notificationManager;
18+
19+
public MainWindow()
20+
{
21+
InitializeComponent();
22+
#if DEBUG
23+
this.AttachDevTools();
24+
#endif
25+
26+
_titleTextBox = this.FindControl<TextBox>("TitleTextBox");
27+
_bodyTextBox = this.FindControl<TextBox>("BodyTextBox");
28+
_eventsListBox = this.FindControl<ListBox>("EventsListBox");
29+
_eventsListBox.Items = new ObservableCollection<string>();
30+
31+
_notificationManager = AvaloniaLocator.Current.GetService<INotificationManager>();
32+
_notificationManager.NotificationActivated += OnNotificationActivated;
33+
_notificationManager.NotificationDismissed += OnNotificationDismissed;
34+
}
35+
36+
private void OnNotificationDismissed(object? sender, NotificationDismissedEventArgs e)
37+
{
38+
((IList<string>) _eventsListBox.Items).Add($"Notification dismissed: {e.Reason}");
39+
}
40+
41+
private void OnNotificationActivated(object? sender, NotificationActivatedEventArgs e)
42+
{
43+
((IList<string>) _eventsListBox.Items).Add($"Notification activated: {e.ActionId}");
44+
}
45+
46+
private void InitializeComponent()
47+
{
48+
AvaloniaXamlLoader.Load(this);
49+
}
50+
51+
public void Button_OnClick(object? sender, RoutedEventArgs e)
52+
{
53+
Debug.Assert(_notificationManager != null);
54+
55+
_notificationManager.ShowNotification(new Notification
56+
{
57+
Title = _titleTextBox.Text ?? _titleTextBox.Watermark,
58+
Body = _bodyTextBox.Text ?? _bodyTextBox.Watermark
59+
});
60+
}
61+
}
62+
}

Example.Avalonia/Program.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Avalonia;
2+
using DesktopNotifications.Avalonia;
3+
4+
namespace Example.Avalonia
5+
{
6+
internal class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
BuildAvaloniaApp()
11+
.StartWithClassicDesktopLifetime(args);
12+
}
13+
14+
public static AppBuilder BuildAvaloniaApp()
15+
{
16+
return AppBuilder.Configure<App>()
17+
.UsePlatformDetect()
18+
.SetupDesktopNotifications()
19+
.LogToTrace();
20+
}
21+
}
22+
}

Example.Avalonia/nuget.config

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!--
4+
To use the Avalonia CI feed to get unstable packages, move this file to the root of your solution.
5+
-->
6+
7+
<configuration>
8+
<packageSources>
9+
<add key="AvaloniaCI" value="https://www.myget.org/F/avalonia-ci/api/v2" />
10+
</packageSources>
11+
</configuration>

0 commit comments

Comments
 (0)