【问题标题】:Showing Windows 8 toast from Windows Forms App从 Windows 窗体应用程序显示 Windows 8 toast
【发布时间】:2014-09-11 15:12:05
【问题描述】:

我们有一个最初为 Windows 7 创建的 Windows 窗体应用程序(与 WPF 相比)。我们正在将该产品向前推进,以便在 Windows 8 中使用。

是否可以从此 WinForm 应用程序为运行 Windows 8 的用户显示 Windows 8 Toast 通知(Windows.UI.Notifications 命名空间)?

我似乎找不到任何例子。我发现的所有东西都是 WPF 或 Windows 应用商店应用程序——没有例子是 WinForm 应用程序。

【问题讨论】:

    标签: winforms windows-8 toast


    【解决方案1】:

    在win 8下可以在winform项目中使用toast通知。我创建一个winform项目,添加一个按钮,当按下按钮时,窗口右上角会显示一个toast通知。以下是我所做的。

    首先需要通过修改cspoj文件打开win 8平台(winform项目中默认关闭),以便添加“Windows”引用。

    在桌面项目中,Core 选项卡默认不会出现。您可以通过右键单击解决方案探索中的项目,选择卸载项目,添加以下sn-p,然后重新打开项目来添加Windows运行时(右键单击项目选择重新加载项目)。当您打开“参考管理器”对话框时,会出现“核心”选项卡。然后你可以在项目中添加“Windows”引用。

    <PropertyGroup>
          <TargetPlatformVersion>8.0</TargetPlatformVersion>
    </PropertyGroup>
    

    更多详情可以参考this link(接近页面末尾部分)

    其次,添加 System.Runtime 引用。

    在“C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll”中手动添加dll(右键引用,添加引用,浏览)

    三、添加toast通知(可以将此代码添加到按钮按下事件中)

    这部分代码可以参考this link 注意:如果你只想显示 toast 通知,你不需要关心 ShellHelpers.cs。或者如果你喜欢,你可以复制下面的代码。你可能需要相应地添加一些使用,并且可能有一张图片,如果没有,它仍然可以运行。哦,你还需要设置一个APP_ID(只是一个const字符串来表示唯一性)。

    private const String APP_ID = "Microsoft.Samples.DesktopToastsSample";
    
    using Windows.UI.Notifications;
    using Windows.Data.Xml.Dom;
    using System.IO;
    
    
    // Get a toast XML template
    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
    
    // Fill in the text elements
    Windows.Data.Xml.Dom.XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
    for (int i = 0; i < stringElements.Length; i++)
    {
        stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
    }
    
    // Specify the absolute path to an image
    String imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
    Windows.Data.Xml.Dom.XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
    imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;
    
    // Create the toast and attach event listeners
    ToastNotification toast = new ToastNotification(toastXml);
    //toast.Activated += ToastActivated;
    //toast.Dismissed += ToastDismissed;
    //toast.Failed += ToastFailed;
    
    // Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
    ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
    

    【讨论】:

    • 您好!您能否详细说明Be sure to specify the AppUserModelId on your application's shortcut!。我正在尝试展示我用 C# 编写的 Windows 窗体(桌面)应用程序的祝酒词。它现在正在显示,但也没有抛出任何异常,我怀疑这个 AppUserModelId 可能是它没有显示的原因。您能帮我理解这是什么,以及应该如何为桌面 (WinForm) 应用程序设置它吗?
    • @Arash 你好,Arash。谢谢你的问题。上面的示例代码(包括 cmets)是从 MS 的 toast 通知示例中复制的。在我的理解中,AppUserModelIDs 只是一个标识应用程序资源的 id(例如 toast 通知窗口、文件)。详细请看document。我对你的情况有点困惑。程序是否抛出异常?还是 toast 通知抛出异常,因此不应该显示?
    • 糟糕!错字! 没有显示,但也没有抛出任何异常。您是否能够获得 Win Form 应用程序展示敬酒?我会审查你附上的文件。谢谢@Jensen。
    • 哦,我手头没有win8操作系统。但是上面的例子绝对可以工作。去年我在实习期间测试了几次。您可以在win8的Metro屏幕中清除与该程序相关的任何内容(如果程序运行正常,它将首次在Metro屏幕中创建一个metro图标)和启动菜单中的项目。然后逐步构建程序。希望它可以提供帮助。 @阿拉什
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2013-09-27
    • 1970-01-01
    相关资源
    最近更新 更多