我正在尝试使用以下代码在 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/