Windows.UI.Notifications is missing
Asked Answered
K

2

19

I want to create simple toast notification to action center in windows 10 from this example. But I got problem on Step 2:

using Windows.UI.Notifications;

It`s missing. But I have spent a lot of time to find it and got no result. I really have no idea where I can find or at least download it.

What I tried:

  1. After long search I found Windows.UI.dll in C:\Windows\System32 but when I try to add it as reference into project I got this error. Even after I tried to copy it and made this fully accessible nothing changed

enter image description here

  1. I tried to reinstall .Net (I`m using 4.5.2)
  2. Installed Windows 10 SDK
  3. Tried to import with global
  4. Added
<PropertyGroup>
      <TargetPlatformVersion>10.0</TargetPlatformVersion>
</PropertyGroup>
  1. Added System.Runtime.dll reference

Example code which probably is useless for you:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.QueryStringDotNET;
using Windows.UI.Notifications;


namespace MessagerClient.Notifications {
    class DefaultWindowsNotification {

        public static void notificationTest() {
            string title = "Andrew sent you a picture";
            string content = "Check this out, Happy Canyon in Utah!";
            string image = "http://blogs.msdn.com/something.jpg";
            string logo = "ms-appdata:///local/Andrew.jpg";
            ToastVisual visual = new ToastVisual() {

                BindingGeneric = new ToastBindingGeneric() {

                    Children =
                    {
                        new AdaptiveText()
                        {
                            Text = title
                        },

                        new AdaptiveText()
                        {
                            Text = content
                        },

                        new AdaptiveImage()
                        {
                            Source = image
                        }
                    },

                    AppLogoOverride = new ToastGenericAppLogo() {
                        Source = logo,
                        HintCrop = ToastGenericAppLogoCrop.Circle
                    }
                }
            };
            Console.WriteLine("NOTIFICATION");
            //Can`t use because of Windows.UI library
            ToastNotificationManager.CreateToastNotifier().Show(visual);
        }
    }
}
Klement answered 25/8, 2016 at 7:39 Comment(2)
Maybe this post could help you.Vine
Please read this article for using UWP notifications in desktop apps.Lytle
E
31

You have to fight Visual Studio pretty hard to use these UWP contracts in a Winforms app. You got off on the wrong foot right away with the wrong TargetPlatformVersion, pretty hard to recover from that. Full steps to take:

Edit the .csproj file with a text editor, Notepad will do. Insert this:

  <PropertyGroup>
       <TargetPlatformVersion>10.0.10586</TargetPlatformVersion>
  </PropertyGroup>

Which assumes you have the 10586 SDK version installed on your machine. Current right now, these versions change quickly. Double-check by looking in the C:\Program Files (x86)\Windows Kits\10\Include with Explorer, you see the installed versions listed in that directory.

Open the Winforms project, use Project > Add Reference > Windows tab > tick the Windows.Data and the Windows.UI contract. Add Reference again and use the Browse tab to select System.Runtime. I picked the one in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\ .NETFramework\v4.6.1\Facades. This reference displays with a warning icon, not sure what it is trying to say but it doesn't appear to have any side-effects.

Test it by dropping a button on the form, double-click to add the Click event handler. The most basic code:

using Windows.UI.Notifications;
...

private void button1_Click(object sender, EventArgs e) {
    var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
    var text = xml.GetElementsByTagName("text");
    text[0].AppendChild(xml.CreateTextNode("Hello world"));
    var toast = new ToastNotification(xml);
    ToastNotificationManager.CreateToastNotifier("anythinggoeshere").Show(toast);
}

Embellish by using a different ToastTemplateType to add an image or more lines of text. Do keep in mind that your program can only work on a Win10 machine.

Excipient answered 25/8, 2016 at 9:5 Comment(1)
I did not use Project > Add Reference > Windows tab > tick the Windows.Data and the Windows.UI contract. After that it works, damn I`m so bad -.-Klement
H
1

If anyone should happen to stumble on this, see this similar but newer post -

Toast Notifications in Win Forms .NET 4.5

Read Stepan Hakobyan's comment at the bottom.

Essentially, I'm seeing the same thing. This code runs, I can step through it line by line with no exceptions but the toast notification is never shown within a Form app.

Hamitic answered 18/1, 2019 at 16:5 Comment(1)
Its never shown? It's THAT GOOD, huh ?!!?Shutz

© 2022 - 2024 — McMap. All rights reserved.