Showing Windows 8 toast from Windows Forms App

前端 未结 1 898
误落风尘
误落风尘 2020-12-31 21:56

We\'ve got a Windows Forms Application (vs. WPF) that was originally created for Windows 7. We\'re carrying the product forward to be used in Windows 8.

Is it possi

相关标签:
1条回答
  • 2020-12-31 22:19

    It is possible to use toast notification in winform poject under win 8. I create a winform project, add a button, when pressing the button, there will be a toast notification showed in the right-top of the window. The following is what I have done.

    First, you need to open win 8 platform by modifying the cspoj file(default closed in winform project), so that you can add "Windows" reference.

    In the desktop projects the Core tab doesn’t appear by default. You can add the Windows Runtime by right click the project in Solution Explore, choosing Unload Project, adding the following snippet, and re-opening the project (right click project choose Reload Project). When you open the Reference Manager dialog box, the Core tab appears. Then you can add "Windows" reference to the project.

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

    For more details you can refer to this link(near to the end part of the page)

    Second, add System.Runtime reference.

    Manually add the dll in "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll" (right click Reference, add Reference, Browse)

    Third, add toast notification (you can add this codes to a button-pressed event)

    This parts of codes you can refer to this link Notes: if you only want to show toast notification, you don't need to care about ShellHelpers.cs. Or if you like, you can just copy the codes below.You may need to add some usings accordingly, and there maybe a picture, if don't have, it can still run. Oh, you also need to set a APP_ID (just a const string to represent uniqueness).

    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);
    
    0 讨论(0)
提交回复
热议问题