Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
anyone know if it is possible to use toast notification (its like notifyicon but windows 8.1 style)

I'm using c#4.0 / visual c# 2010 express / desktop app (not WPF)
is possible to use? how is it?
Posted
Updated 1-Jan-14 10:35am
v2

1 solution

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:
C#
namespace BurntToast
{
    class Program
    {
        static void Main(string[] args)
        {
            MakeToast("Ooops.", "You burnt the toast.");
        }

        /// <summary>
        /// Makes toast.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="text">The text.</param>
        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.
 
Share this answer
 
Comments
BillWoodruff 1-Jan-14 20:29pm    
+5 Excellent answer !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900