问题
I got a Windows Forms project in C# with a NotifyIcon. I'm using the following code to display a balloon notification:
notifyIcon.ShowBalloonTip(1000, "title", "text", ToolTipIcon.Info);
This was working fine until Windows 8.1. Now I've installed the Windows 10 Preview and the balloon notification doesn't show up anymore.
I guess that all notifications are moved to the Windows 8 style toast notifications and the balloon notifications are completely removed (because I haven't seen a single balloon notification yet and many more toast notifications), however I haven't found an official source for this yet.
The problem is that my application is simply a single .exe file and so it doesn't have an installer or shortcut. According to this page an installer that creates a shortcut is needed for toast notifications to work.
How can I show notifications (I don't care if it's a balloon or toast notification) in Windows 10 without any shortcuts or installers?
回答1:
I used this thread to help make this code - I am sharing my success.
warning I am kind of new to C# so my code probably sucks- but it does work and is pretty simplistic and that's more than I can say for most solutions I have found
Also I was having a hell of a time getting the xml document to read. I was fighting with System.xml (I think) and Windows.Data.Dom.Xml (also not completely sure). In the end I settled on making them hard coded strings for my example file and used a switch statement to switch between them. I have found a ton of people, looking for the solution that I have come up with, on stack overflow. It seems use of the toast notification system with console or background applications would be super useful, and the documentation that surrounds the toast notification system with windows applications all suggest that it needs to be used with an application. The Action Center is super useful for notifications vrs the NotificationTray/NotifyIcon route. I have not found a full solution anywhere else on the web. Here is example code.
/*
At first you need to declare that your program will be using winRT libraries:
1. Right click on your yourProject, select Unload Project
2. Right click on your youProject(unavailable) and click Edit yourProject.csproj
3. Add a new property group:<TargetPlatformVersion>8.0</TargetPlatformVersion>
4. Reload project
5. Add referece Windows from Windows > Core
*/
using System;
using Windows.Data.Xml.Dom;
using Windows.Storage;
using Windows.Storage.Streams;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Notifications;
namespace ConsoleApplication6
{
public class NewToastNotification
{
public NewToastNotification(string input, int type)
{
string NotificationTextThing = input;
string Toast = "";
switch (type)
{
case 1:
{
//Basic Toast
Toast = "<toast><visual><binding template=\"ToastImageAndText01\"><text id = \"1\" >";
Toast += NotificationTextThing;
Toast += "</text></binding></visual></toast>";
break;
}
default:
{
Toast = "<toast><visual><binding template=\"ToastImageAndText01\"><text id = \"1\" >";
Toast += "Default Text String";
Toast += "</text></binding></visual></toast>";
break;
}
}
XmlDocument tileXml = new XmlDocument();
tileXml.LoadXml(Toast);
var toast = new ToastNotification(tileXml);
ToastNotificationManager.CreateToastNotifier("New Toast Thing").Show(toast);
}
}
class Program
{
static void Main(string[] args)
{
NewToastNotification Window = new NewToastNotification("Yes",1);
}
}
}
回答2:
Well, after the last update of the Technical Preview (the one with a notification center next to the system tray) it's working.
My notifications are showing like usual, without any code changes. They're just like normal Windows 8 style toast notifications, except these show up at the bottom.
回答3:
This solution worked for me:
- Open gpedit.msc
- User Configuration -> Administrative Templates -> Start Menu and Taskbar -> "Disable showing balloon notifications as toasts" set to "Enabled"
- Restart the Windows
来源:https://stackoverflow.com/questions/26192333/notifications-on-windows-10-using-desktop-app