Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows.UI.Notifications is missing

Tags:

c#

winforms

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);
        }
    }
}
like image 574
Andrey Danilov Avatar asked Aug 25 '16 07:08

Andrey Danilov


People also ask

How do I create a Windows UI notification?

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.

What is Windows System toast?

Toast notifications are similar to a popup message. These notifications provide time-sensitive information about events that occur while an application is running. Toast notifications appear in the foreground whether Windows is currently in desktop mode, displaying the lock screen, or running another application.

What is Microsoft toast notification?

A toast notification is a message that your app can construct and deliver to your user while they are not currently inside your app. This quickstart walks you through the steps to create, deliver, and display a Windows 10 or Windows 11 toast notification using rich content and interactive actions.


2 Answers

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.

like image 143
Hans Passant Avatar answered Oct 04 '22 13:10

Hans Passant


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.

like image 24
Kevin Moore Avatar answered Oct 04 '22 13:10

Kevin Moore