![](/img/trans.png)
[英]Unexpected behaviours in toast notification from desktop app in Windows 10
[英]How to receive textbox value from a Windows Toast Notificiation in a desktop app?
我正在用 C# 編寫一個 Windows 10 桌面應用程序(不是通用應用程序,這很重要),它需要發送一個 toast 通知,其中包括一個文本框和一個提交按鈕。
我使用ToastNotification.Activated來監聽點擊提交按鈕的時間,但我找不到任何方法從 Toast 中提取文本框文本。
所有我已經找到了方法-包括MSFT文章在這里和給出的答案StackOverflow的這里,似乎只適用於通用的應用程序,我不能將其應用到桌面應用程序。
關於如何從 Win10桌面應用程序的吐司中提取文本框文本輸入的任何線索?
在下面附上我的代碼片段
謝謝
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.