Skip to content

Commit bbbdcf3

Browse files
loneursidEric Langlois
andauthored
User/erlangl/samples/toast c# winui unpackaged (#203)
* Initial empty sample * matching naming of the cpp sample * replaced with new app notifications sample * There's only one scenario for now * Implementing scenario 1 * code cleanup * Adding second scenario * Update to preview3 SDK * Can show a toast * Scenario 2 can show toast * alignment * Seup a notification handler * wip * Route received notifications to the correct scenario * Scenario formats received notifications and pass to UI * Adding support for toast notification activation * Scenario descriptions were flipped * Temp workaround listview issue * Fixing path to icon * Fixing issue with updating received activations in the UI * Ensuring Unregister is called on app exit * Code clean-up * Update to match C++ sample * Code clean-up * code clean-up * nits * Bringing the sample to par with the C++ one * Adding launch arguments to toasts * Adding comment * Bringing this sample to par with its cpp counterpart * Simplify interop call Co-authored-by: Eric Langlois <[email protected]>
1 parent 3f12ead commit bbbdcf3

35 files changed

+1346
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32228.430
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CsUnpackagedAppNotifications", "CsUnpackagedAppNotifications\CsUnpackagedAppNotifications.csproj", "{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|arm64 = Debug|arm64
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|arm64 = Release|arm64
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Debug|arm64.ActiveCfg = Debug|arm64
19+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Debug|arm64.Build.0 = Debug|arm64
20+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Debug|arm64.Deploy.0 = Debug|arm64
21+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Debug|x64.ActiveCfg = Debug|x64
22+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Debug|x64.Build.0 = Debug|x64
23+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Debug|x64.Deploy.0 = Debug|x64
24+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Debug|x86.ActiveCfg = Debug|x86
25+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Debug|x86.Build.0 = Debug|x86
26+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Debug|x86.Deploy.0 = Debug|x86
27+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Release|arm64.ActiveCfg = Release|arm64
28+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Release|arm64.Build.0 = Release|arm64
29+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Release|arm64.Deploy.0 = Release|arm64
30+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Release|x64.ActiveCfg = Release|x64
31+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Release|x64.Build.0 = Release|x64
32+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Release|x64.Deploy.0 = Release|x64
33+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Release|x86.ActiveCfg = Release|x86
34+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Release|x86.Build.0 = Release|x86
35+
{57575E66-F0BD-47E1-948B-B7E9D98AC0B0}.Release|x86.Deploy.0 = Release|x86
36+
EndGlobalSection
37+
GlobalSection(SolutionProperties) = preSolution
38+
HideSolutionNode = FALSE
39+
EndGlobalSection
40+
GlobalSection(ExtensibilityGlobals) = postSolution
41+
SolutionGuid = {358A2C21-4A12-4CC5-B7C3-7B6E3E53C6CA}
42+
EndGlobalSection
43+
EndGlobal
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!-- Copyright (c) Microsoft Corporation.
2+
Licensed under the MIT License. -->
3+
<Application
4+
x:Class="CsUnpackagedAppNotifications.App"
5+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
6+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
7+
xmlns:local="using:CsUnpackagedAppNotifications">
8+
9+
<Application.Resources>
10+
<!-- Application-specific resources -->
11+
<ResourceDictionary>
12+
<ResourceDictionary.MergedDictionaries>
13+
<!--
14+
Styles that define common aspects of the platform look and feel
15+
Required by Visual Studio project and item templates
16+
-->
17+
<ResourceDictionary Source="ms-appx:///Styles.xaml"/>
18+
</ResourceDictionary.MergedDictionaries>
19+
</ResourceDictionary>
20+
</Application.Resources>
21+
</Application>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Windows.AppLifecycle;
5+
using Microsoft.UI.Xaml;
6+
using Microsoft.Windows.AppNotifications;
7+
using Windows.Win32.Foundation;
8+
using System;
9+
using CsUnpackagedAppNotifications.Notifications;
10+
using static Windows.Win32.PInvoke;
11+
12+
namespace CsUnpackagedAppNotifications
13+
{
14+
15+
public partial class App : Application
16+
{
17+
private static Window mainWindow = null;
18+
private NotificationManager notificationManager;
19+
20+
public App()
21+
{
22+
this.InitializeComponent();
23+
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);
24+
25+
notificationManager = new NotificationManager();
26+
}
27+
28+
public static void ToForeground()
29+
{
30+
if (mainWindow != null)
31+
{
32+
HWND hwnd = (HWND)WinRT.Interop.WindowNative.GetWindowHandle(mainWindow);
33+
SwitchToThisWindow(hwnd, true);
34+
}
35+
}
36+
37+
public static string GetFullPathToExe()
38+
{
39+
var path = AppDomain.CurrentDomain.BaseDirectory;
40+
var pos = path.LastIndexOf("\\");
41+
return path.Substring(0, pos);
42+
}
43+
44+
public static string GetFullPathToAsset(string assetName)
45+
{
46+
return GetFullPathToExe() + "\\Assets\\" + assetName;
47+
}
48+
49+
protected override void OnLaunched(LaunchActivatedEventArgs args)
50+
{
51+
mainWindow = new MainWindow();
52+
53+
notificationManager.Init();
54+
55+
// NOTE: AppInstance is ambiguous between
56+
// Microsoft.Windows.AppLifecycle.AppInstance and
57+
// Windows.ApplicationModel.AppInstance
58+
var currentInstance = AppInstance.GetCurrent();
59+
if (currentInstance.IsCurrent)
60+
{
61+
// AppInstance.GetActivatedEventArgs will report the correct ActivationKind,
62+
// even in WinUI's OnLaunched.
63+
AppActivationArguments activationArgs = currentInstance.GetActivatedEventArgs();
64+
if (activationArgs != null)
65+
{
66+
ExtendedActivationKind extendedKind = activationArgs.Kind;
67+
if (extendedKind == ExtendedActivationKind.AppNotification)
68+
{
69+
var notificationActivatedEventArgs = (AppNotificationActivatedEventArgs)activationArgs.Data;
70+
notificationManager.ProcessLaunchActivationArgs(notificationActivatedEventArgs);
71+
}
72+
}
73+
}
74+
75+
mainWindow.Activate();
76+
}
77+
78+
void OnProcessExit(object sender, EventArgs e)
79+
{
80+
notificationManager.Unregister();
81+
}
82+
}
83+
}
76.3 KB
Loading
10.6 KB
Loading
2.73 KB
Loading
10.9 KB
Loading
3.06 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>WinExe</OutputType>
4+
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
5+
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
6+
<RootNamespace>CsUnpackagedAppNotifications</RootNamespace>
7+
<ApplicationManifest>app.manifest</ApplicationManifest>
8+
<Platforms>x86;x64;arm64</Platforms>
9+
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
10+
<UseWinUI>true</UseWinUI>
11+
<EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
12+
<PublishProfile>Properties\PublishProfiles\win10-$(Platform).pubxml</PublishProfile>
13+
<WindowsPackageType>None</WindowsPackageType>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.194" />
18+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.588-beta" PrivateAssets="all" />
19+
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.0-preview3" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<Manifest Include="$(ApplicationManifest)" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<Content Include="Assets\SplashScreen.png" />
28+
<Content Include="Assets\Square150x150Logo.png" />
29+
<Content Include="Assets\Square44x44Logo.png" />
30+
<Content Include="Assets\logo.png" />
31+
<Content Include="Assets\Wide310x150Logo.png" />
32+
<Content Include="Assets\windows-sdk.ico" />
33+
</ItemGroup>
34+
35+
<!-- Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
36+
Tools extension to be activated for this project even if the Windows App SDK Nuget
37+
package has not yet been restored -->
38+
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnablePreviewMsixTooling)'=='true'">
39+
<ProjectCapability Include="Msix" />
40+
</ItemGroup>
41+
</Project>

0 commit comments

Comments
 (0)