Add Avalonia example project

This commit is contained in:
Luis von der Eltz
2021-02-07 13:54:08 +01:00
parent 088b16a707
commit 8e782e8b73
10 changed files with 246 additions and 1 deletions
@@ -0,0 +1,55 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform;
using DesktopNotifications.FreeDesktop;
using DesktopNotifications.Windows;
namespace DesktopNotifications.Avalonia
{
/// <summary>
/// Extensions for <see cref="AppBuilderBase{TAppBuilder}"/>
/// </summary>
public static class AppBuilderExtensions
{
/// <summary>
/// Setups the <see cref="INotificationManager"/> for the current platform and
/// binds it to the service locator (<see cref="AvaloniaLocator"/>).
/// </summary>
/// <typeparam name="TAppBuilder"></typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static TAppBuilder SetupDesktopNotifications<TAppBuilder>(this TAppBuilder builder)
where TAppBuilder : AppBuilderBase<TAppBuilder>, new()
{
return builder.AfterSetup(async b =>
{
var runtimeInfo = b.RuntimePlatform.GetRuntimeInfo();
INotificationManager manager;
switch (runtimeInfo.OperatingSystem)
{
case OperatingSystemType.WinNT:
{
var context = WindowsApplicationContext.FromCurrentProcess(b.Instance.Name);
manager = new WindowsNotificationManager(context);
break;
}
case OperatingSystemType.Linux:
{
var context = FreeDesktopApplicationContext.FromCurrentProcess();
manager = new FreeDesktopNotificationManager(context);
break;
}
//TODO: OSX once implemented/stable
default: return;
}
await manager.Initialize();
AvaloniaLocator.CurrentMutable.Bind<INotificationManager>().ToConstant(manager);
});
}
}
}
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DesktopNotifications.FreeDesktop\DesktopNotifications.FreeDesktop.csproj" />
<ProjectReference Include="..\DesktopNotifications.Windows\DesktopNotifications.Windows.csproj" />
<ProjectReference Include="..\DesktopNotifications\DesktopNotifications.csproj" />
</ItemGroup>
</Project>
+13 -1
View File
@@ -11,7 +11,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example", "Example\Example.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopNotifications.FreeDesktop", "DesktopNotifications.FreeDesktop\DesktopNotifications.FreeDesktop.csproj", "{96F637EF-FD11-41F5-8F40-4E37313697E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesktopNotifications.Apple", "DesktopNotifications.Apple\DesktopNotifications.Apple.csproj", "{B4A6C639-79C4-48EF-955F-273CF01D7770}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopNotifications.Apple", "DesktopNotifications.Apple\DesktopNotifications.Apple.csproj", "{B4A6C639-79C4-48EF-955F-273CF01D7770}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesktopNotifications.Avalonia", "DesktopNotifications.Avalonia\DesktopNotifications.Avalonia.csproj", "{5D9904D2-E102-409D-AAFE-394A3DED40D9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.Avalonia", "Example.Avalonia\Example.Avalonia.csproj", "{4408FE39-C5AF-453D-B6EE-E1A42504264A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -39,6 +43,14 @@ Global
{B4A6C639-79C4-48EF-955F-273CF01D7770}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4A6C639-79C4-48EF-955F-273CF01D7770}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4A6C639-79C4-48EF-955F-273CF01D7770}.Release|Any CPU.Build.0 = Release|Any CPU
{5D9904D2-E102-409D-AAFE-394A3DED40D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D9904D2-E102-409D-AAFE-394A3DED40D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D9904D2-E102-409D-AAFE-394A3DED40D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D9904D2-E102-409D-AAFE-394A3DED40D9}.Release|Any CPU.Build.0 = Release|Any CPU
{4408FE39-C5AF-453D-B6EE-E1A42504264A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4408FE39-C5AF-453D-B6EE-E1A42504264A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4408FE39-C5AF-453D-B6EE-E1A42504264A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4408FE39-C5AF-453D-B6EE-E1A42504264A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
+7
View File
@@ -0,0 +1,7 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Example.Avalonia.App">
<Application.Styles>
<FluentTheme Mode="Light"/>
</Application.Styles>
</Application>
+24
View File
@@ -0,0 +1,24 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
namespace Example.Avalonia
{
public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow();
}
base.OnFrameworkInitializationCompleted();
}
}
}
+15
View File
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.0" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.0" />
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DesktopNotifications.Avalonia\DesktopNotifications.Avalonia.csproj" />
</ItemGroup>
</Project>
+20
View File
@@ -0,0 +1,20 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="400"
Width="400" Height="400"
x:Class="Example.Avalonia.MainWindow"
Title="Example.Avalonia">
<StackPanel Margin="20" Spacing="10">
<TextBox Name="TitleTextBox" Watermark="Title" UseFloatingWatermark="True" />
<TextBox Name="BodyTextBox" Watermark="Body" UseFloatingWatermark="True" />
<Button Click="Button_OnClick" Content="Show Notification" />
<TextBlock Foreground="Gray">Events:</TextBlock>
<ListBox Name="EventsListBox" Height="200" />
</StackPanel>
</Window>
+62
View File
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using DesktopNotifications;
namespace Example.Avalonia
{
public class MainWindow : Window
{
private readonly TextBox _bodyTextBox;
private readonly ListBox _eventsListBox;
private readonly TextBox _titleTextBox;
private readonly INotificationManager _notificationManager;
public MainWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
_titleTextBox = this.FindControl<TextBox>("TitleTextBox");
_bodyTextBox = this.FindControl<TextBox>("BodyTextBox");
_eventsListBox = this.FindControl<ListBox>("EventsListBox");
_eventsListBox.Items = new ObservableCollection<string>();
_notificationManager = AvaloniaLocator.Current.GetService<INotificationManager>();
_notificationManager.NotificationActivated += OnNotificationActivated;
_notificationManager.NotificationDismissed += OnNotificationDismissed;
}
private void OnNotificationDismissed(object? sender, NotificationDismissedEventArgs e)
{
((IList<string>) _eventsListBox.Items).Add($"Notification dismissed: {e.Reason}");
}
private void OnNotificationActivated(object? sender, NotificationActivatedEventArgs e)
{
((IList<string>) _eventsListBox.Items).Add($"Notification activated: {e.ActionId}");
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
public void Button_OnClick(object? sender, RoutedEventArgs e)
{
Debug.Assert(_notificationManager != null);
_notificationManager.ShowNotification(new Notification
{
Title = _titleTextBox.Text ?? _titleTextBox.Watermark,
Body = _bodyTextBox.Text ?? _bodyTextBox.Watermark
});
}
}
}
+22
View File
@@ -0,0 +1,22 @@
using Avalonia;
using DesktopNotifications.Avalonia;
namespace Example.Avalonia
{
internal class Program
{
public static void Main(string[] args)
{
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
public static AppBuilder BuildAvaloniaApp()
{
return AppBuilder.Configure<App>()
.UsePlatformDetect()
.SetupDesktopNotifications()
.LogToTrace();
}
}
}
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
To use the Avalonia CI feed to get unstable packages, move this file to the root of your solution.
-->
<configuration>
<packageSources>
<add key="AvaloniaCI" value="https://www.myget.org/F/avalonia-ci/api/v2" />
</packageSources>
</configuration>