Not without third-party libraries.
You would normally do this by
referencing the WinRT runtime[
^], and then making the API calls to show the toast:
namespace BurntToast
{
class Program
{
static void Main(string[] args)
{
MakeToast("Ooops.", "You burnt the toast.");
}
private static ToastNotification MakeToast(string title, string text)
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
XmlElement toastNode = toastXml.DocumentElement;
XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].InnerText = title;
toastTextElements[1].InnerText = text;
toastNode.SetAttribute("duration", "long");
var toast = new ToastNotification(toastXml);
var notifier = ToastNotificationManager.CreateToastNotifier();
notifier.Show(toast);
return toast;
}
}
}
This code will compile and execute; however when
CreateToastNotifier()
is invoked you will get an unintuitive exception like this:
at Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier()
at BurntToast.Program.MakeToast(String title, String text) in f:\Dropbox\Red Cell Innovation\Development\BurntToast\BurntToast\Program.cs:line 28
at BurntToast.Program.Main(String[] args) in f:\Dropbox\Red Cell Innovation\Development\BurntToast\BurntToast\Program.cs:line 11
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
If you read between the lines, the problem is actually that the runtime cannot find the
VisualElements
element in the Windows Store application manifest (because this manifest doesn't exist). This element would contain the required
ToastCapable="true"
attribute. The only way to make it work is to actually make the application a Windows Store app.
With some searching you will find many third-party components that simulate toast notifications for non-Windows Store applications.