Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

c# - Windows.UI.Notifications is missing

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:WindowsSystem32 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);
        }
    }
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 Kits10Include 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 AssembliesMicrosoftFramework .NETFrameworkv4.6.1Facades. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...