c# - Toast 不从 C# 中的控制台应用程序显示

标签 c# toast

我正在尝试使用以下代码在 C# 中从控制台应用程序实现 Toast 通知。

我也尝试了 wpf 代码,它工作得很好。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Notifications;
using Microsoft.Toolkit.Uwp.Notifications; // Notifications library
using Windows.Data.Xml.Dom;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // In a real app, these would be initialized with actual data
            string title = "Andrew sent you a picture";
            string content = "Check this out, Happy Canyon in Utah!";
            string image = "http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-81-permanent/2727.happycanyon1_5B00_1_5D00_.jpg";
            string logo = "ms-appdata:///local/Andrew.jpg";
            int conversationId = 384928;

            // Construct the visuals of the toast
            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
                    }
                }
            };

            // Construct the actions for the toast (inputs and buttons)
            ToastActionsCustom actions = new ToastActionsCustom()
            {
                Inputs =
                {
                    new ToastTextBox("tbReply")
                    {
                        PlaceholderContent = "Type a response"
                    }
                }

            };


            // Now we can construct the final toast content
            ToastContent toastContent = new ToastContent()
            {
                Visual = visual,
                Actions = actions,

                // Arguments when the user taps body of toast

            };
            //Console.WriteLine(toastContent.GetContent());
            //Console.ReadKey();
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(toastContent.GetContent());


            // And create the toast notification
            ToastNotification notification = new ToastNotification(xDoc);


            // And then send the toast
            ToastNotificationManager.CreateToastNotifier("Test").Show(notification);
        }
    }
}

我没有在代码中看到任何错误,但由于某种原因,Toast 没有出现。

相同的代码与 WPF 应用程序完美配合。

任何人都可以帮我解决我在这里缺少的东西吗?

最佳答案

我在控制台应用程序中也遇到了同样的问题,我使用了 Notifications Visualizer 中的示例代码应用程序。此代码没有 applicationId 字符串并崩溃,正如 this question 中所述。 .

但是添加了一个applicationId之后,没有任何反应。没有编译错误,也没有引发异常。结果你现在必须使用 ToastNotificationManagerCompat类而不是 ToastNotificationManager

替换:

// And then send the toast
ToastNotificationManager.CreateToastNotifier("Test").Show(notification);

与:

// And then send the toast
ToastNotificationManagerCompat.CreateToastNotifier().Show(notification);

关于c# - Toast 不从 C# 中的控制台应用程序显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56154079/

相关文章:

c# - ASP.NET MVC 中的动态路由操作名称

c# - 在 .NET 中依赖 && 短路安全吗?

android - 如何设置Kotlin中的Toast的重力(属性样式)(出现错误)?

c# - Interop 导致 Unity 崩溃

c# - 将 BitmapImage 加载到 WriteableBitmap 但不存在方法

c# - 如何使 C# 中的侵入式树类使用泛型?

java - Toast 消息不显示

java - Android toast 更改偏好设置

Android 在设置时间后隐藏对话框,就像自定义时间间隔 toast

android - Toast 消息要求用户填写空白 "EditText"字段