自从Windows 8.1,我就在桌面应用中使用toast通知了,但是在Windows 10的新操作中心中,我遇到了一些意外的行为。
当用户对toast不做任何操作时,它就会简单地消失而不进入操作中心(ToastNotification.Dismissed
是ToastDismissalReason.TimedOut
)。我不知道这是否与我在win32应用程序中使用它有关,但是在Windows Universal App中相同的toast在超时时进入操作中心。
需要注意的一点是,我没有为我的win32应用程序注册AppUserModelID,就像在W8.1中所需的那样,现在似乎不再是强制性的。我仍然测试了一个已注册的id,并且我遇到了相同的问题。
那么,如何防止toast在超时时未进入操作中心?
下面是一个最小化代码(控制台应用程序),可重现此问题:
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
namespace ToastDesktop
{
internal class Program
{
/// Add in the .csproj in the <PropertyGroup/> where <TargetFrameworkVersion/> is:
/// <TargetPlatformVersion>10.0.10240.0</TargetPlatformVersion>
///
/// Reference to add :
/// - Windows.UI
/// - Windows.Data
private static void Main(string[] args)
{
string xml = $@"
<toast>
<visual>
<binding template='ToastGeneric'>
<text>Some title</text>
<text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</text>
</binding>
</visual>
</toast>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
ToastNotification toast = new ToastNotification(doc);
toast.Tag = "tag";
toast.Group = "group";
ToastNotificationManager.CreateToastNotifier("ToastDesktop").Show(toast);
}
}
}
感谢您的帮助。
编辑:我在涉及此问题的msdn博客文章上发布了这个错误,并得到确认,它应该在超时后保留在操作中心,这可能是一个错误。