/
MainWindow.xaml.cs
169 lines (144 loc) · 6.44 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using DesktopToastsSample.ShellHelpers;
using Microsoft.Toolkit.Uwp.Notifications;
namespace DesktopToastsSample
{
public partial class MainWindow : Window
{
private const String APP_ID = "Microsoft.Samples.DesktopToasts";
public MainWindow()
{
InitializeComponent();
RegisterAppForNotificationSupport();
NotificationActivator.Initialize();
ShowToastButton.Click += ShowToastButton_Click;
this.Closing += CloseMainWindow;
}
public void ToastActivated()
{
Dispatcher.Invoke(() =>
{
// Open the window if it was minimized
if (WindowState == WindowState.Minimized)
{
WindowState = WindowState.Normal;
}
// And then activate the window
Activate();
Output.Text = "The user activated the toast.";
});
}
private void CloseMainWindow(object sender, CancelEventArgs e)
{
NotificationActivator.Uninitialize();
}
// In order to display toasts, a desktop application must have a shortcut on the Start menu.
// Also, an AppUserModelID must be set on that shortcut.
//
// For the app to be activated from Action Center, it needs to register a COM server with the OS
// and register the CLSID of that COM server on the shortcut.
//
// The shortcut should be created as part of the installer. The following code shows how to create
// a shortcut and assign the AppUserModelID and ToastActivatorCLSID properties using Windows APIs.
//
// Included in this project is a wxs file that be used with the WiX toolkit
// to make an installer that creates the necessary shortcut. One or the other should be used.
//
// This sample doesn't clean up the shortcut or COM registration.
private void RegisterAppForNotificationSupport()
{
String shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Windows\\Start Menu\\Programs\\Nitro Desktop Toasts Sample CS.lnk";
if (!File.Exists(shortcutPath))
{
// Find the path to the current executable
String exePath = Process.GetCurrentProcess().MainModule.FileName;
InstallShortcut(shortcutPath, exePath);
RegisterComServer(exePath);
}
}
private void InstallShortcut(String shortcutPath, String exePath)
{
IShellLinkW newShortcut = (IShellLinkW)new CShellLink();
// Create a shortcut to the exe
newShortcut.SetPath(exePath);
// Open the shortcut property store, set the AppUserModelId property
IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut;
PropVariantHelper varAppId = new PropVariantHelper();
varAppId.SetValue(APP_ID);
newShortcutProperties.SetValue(PROPERTYKEY.AppUserModel_ID, varAppId.Propvariant);
PropVariantHelper varToastId = new PropVariantHelper();
varToastId.VarType = VarEnum.VT_CLSID;
varToastId.SetValue(typeof(NotificationActivator).GUID);
newShortcutProperties.SetValue(PROPERTYKEY.AppUserModel_ToastActivatorCLSID, varToastId.Propvariant);
// Commit the shortcut to disk
ShellHelpers.IPersistFile newShortcutSave = (ShellHelpers.IPersistFile)newShortcut;
newShortcutSave.Save(shortcutPath, true);
}
private void RegisterComServer(String exePath)
{
// We register the app process itself to start up when the notification is activated, but
// other options like launching a background process instead that then decides to launch
// the UI as needed.
string regString = String.Format("SOFTWARE\\Classes\\CLSID\\{{{0}}}\\LocalServer32", typeof(NotificationActivator).GUID);
var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(regString);
key.SetValue(null, exePath);
}
// Create and show the toast.
// See the "Toasts" sample for more detail on what can be done with toasts
private void ShowToastButton_Click(object sender, RoutedEventArgs e)
{
// Create the toast content
ToastContent content = new ToastContent()
{
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = "Hello World"
},
new AdaptiveText()
{
Text = "This is a Win32 toast notification!"
}
},
AppLogoOverride = new ToastGenericAppLogo()
{
// Specify the absolute path to an image as a URI
Source = new System.Uri(Path.GetFullPath("toastImageAndText.png")).AbsoluteUri
}
}
}
};
// Create XML from the toast content
XmlDocument xml = new XmlDocument();
xml.LoadXml(content.GetContent());
// Create the toast and attach event listeners
ToastNotification toast = new ToastNotification(xml);
toast.Failed += ToastFailed;
// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
}
private void ToastActivated(ToastNotification sender, object e)
{
ToastActivated();
}
private void ToastFailed(ToastNotification sender, ToastFailedEventArgs e)
{
Dispatcher.Invoke(() =>
{
Output.Text = "The toast encountered an error.";
});
}
}
}