Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notification functionality from WinAppSDK #543

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sample Applications/Win11ThemeGallery/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public partial class App : Application

services.AddTransient<UserDashboardPage>();
services.AddTransient<UserDashboardPageViewModel>();

services.AddTransient<NotificationsPage>();
services.AddTransient<NotificationsPageViewModel>();
services.AddSingleton<SettingsPage>();
services.AddSingleton<SettingsPageViewModel>();
}).Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public partial class MainWindowViewModel : ObservableObject
new NavigationItem("PasswordBox", typeof(PasswordBoxPage)),
}
},
new NavigationItem
{
Name = "Notifications",
PageType = typeof(NotificationsPage),
Icon = "\xE8D2"
},
};

[ObservableProperty]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Win11ThemeGallery.ViewModels
{
public partial class NotificationsPageViewModel
{
private static AppNotificationManager notificationManager;

static NotificationsPageViewModel()
{
notificationManager = AppNotificationManager.Default;
notificationManager.NotificationInvoked += OnNotificationInvoked;
notificationManager.Register();
}

private static void OnNotificationInvoked(AppNotificationManager sender, AppNotificationActivatedEventArgs args)
{
throw new NotImplementedException();
}

[RelayCommand]
public void SendNotification(object pageType)
{

var appNotification = new AppNotificationBuilder()
.AddArgument("action", "ToastClick")
.AddText("Notification From Gallery App")
.AddText("This is an example message using WPF")
.AddButton(new AppNotificationButton("Open App")
.AddArgument("action", "OpenApp")
)
.BuildNotification();
notificationManager.Show(appNotification);

}
}
}
14 changes: 14 additions & 0 deletions Sample Applications/Win11ThemeGallery/Views/NotificationsPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Page x:Class="Win11ThemeGallery.Views.NotificationsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Win11ThemeGallery.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="NotificationsPage">

<Grid>
<Button Width="200" Height="100" Command="{Binding ViewModel.SendNotificationCommand, RelativeSource={RelativeSource AncestorType={x:Type local:NotificationsPage}}}" CommandParameter="{Binding PageType}">Send Notification</Button>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Win11ThemeGallery.ViewModels;
namespace Win11ThemeGallery.Views
{
/// <summary>
/// Interaction logic for NotificationsPage.xaml
/// </summary>
public partial class NotificationsPage : Page
{
public NotificationsPageViewModel ViewModel { get; }
public NotificationsPage(NotificationsPageViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
DataContext = this;
}
}
}
18 changes: 16 additions & 2 deletions Sample Applications/Win11ThemeGallery/Win11ThemeGallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<TargetFramework>net9.0-windows10.0.19041.0</TargetFramework>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, we're bringing a fix to WinAppSDK 1.5 that should make this change unnecessary, it may be released this week. Once it's ready, let's use it and see if we can change this back to "net9.0-windows", etc.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this out locally, it looks like we'll need to keep this like you have it for now :(

<RuntimeIdentifiers>win-x64;</RuntimeIdentifiers>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
<WindowsPackageType>None</WindowsPackageType>
<UseRidGraph>true</UseRidGraph>
<PlatformTarget>x64</PlatformTarget>
<Platforms>AnyCPU;x64</Platforms>
<StartupObject>Win11ThemeGallery.App</StartupObject>
Expand All @@ -24,7 +27,18 @@
<Resource Include="Assets\win11-dashboard.png" />
<Resource Include="Assets\*-100x100.jpg" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.2.164-beta">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.0" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />
</ItemGroup>
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnablePreviewMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>
<PropertyGroup>
<WpfRepoRoot>E:\Wpf</WpfRepoRoot>
</PropertyGroup>
Expand Down
Loading