我正在使用 C# 编写一个 Windows 10 桌面应用程序(不是通用应用程序,这很重要),它需要发送 toast 通知,其中包括一个文本框和一个提交按钮。
我使用 ToastNotification.Activated在单击提交按钮时收听,但我找不到任何方法从 toast 中提取文本框文本。
我找到的所有方法 - 包括 MSFT 文章 here以及给出的 StackOverflow 答案 here ,似乎只适用于通用应用程序,我无法将它们应用到桌面应用程序。
关于如何从 Win10 桌面 应用程序的 toast 中提取文本框文本输入的任何线索?
在下面附上我的代码片段
谢谢
private void ShowToastButton_Click(object sender, RoutedEventArgs e)
{
string toastXmlString=$@"
<toast action='submit'>
<visual>
<binding template='ToastGeneric'>
<text hint-maxLines='1'>Line1</text>
<text>Line2</text>
</binding>
</visual>
<actions>
<input id='textBox' type='text' placeHolderContent='text' />
<action
content='Submit'
hint-inputId='textBox'
arguments='action=text' />
</actions>
</toast>";
Windows.Data.Xml.Dom.XmlDocument toastCustomtXml = new Windows.Data.Xml.Dom.XmlDocument();
toastCustomtXml.LoadXml(toastXmlString);
// Create the toast and attach event listeners
ToastNotification toast = new ToastNotification(toastCustomtXml);
toast.Activated += ToastActivated;
toast.Dismissed += ToastDismissed;
toast.Failed += ToastFailed;
// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
}
private void ToastActivated(ToastNotification sender, object e)
{
// This event is fired, but I can't extract the textbox text
// 'e' is a ToastActivatedEventArgs object, e.Arguments == 'action=text'
Dispatcher.Invoke(() =>
{
Activate();
Output.Text = "The user activated the toast.";
});
}
最佳答案
在您的 App.xaml.cs 文件中添加一个覆盖 void 来处理 OnActivated。
protected async override void OnActivated(IActivatedEventArgs e)
{
// Handle toast activation
if (e is ToastNotificationActivatedEventArgs)
{
var toastActivationArgs = e as ToastNotificationActivatedEventArgs;
string text = toastActivationArgs.UserInput.Values.ToList()[0].ToString());
}
Window.Current.Activate();
}
关于c# - 如何从桌面应用程序中的 Windows Toast 通知接收文本框值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39556471/