c# - 如何从桌面应用程序中的 Windows Toast 通知接收文本框值?

标签 c# windows notifications windows-10 desktop-application

我正在使用 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/

相关文章:

c++ - 将 win32 应用程序图标添加到任务栏

windows - 在 Windows 上编译用 Zig 编写的 CPython 扩展

windows - 'Language neutral' 对于 MAKELANGID 意味着什么?

c# - 以编程方式打开/关闭操作中心

c# - 连接未打开: MySql BeginTransaction

c# - 即使值为 null,StringValues 计数也显示 1

c# - 在 C# 中获取命名空间中的类列表

swift - 如何在检测到 iBeacon、应用程序正在运行和关闭时向用户显示通知?

flutter - 通知图标未显示在 flutter android 应用程序 local_notification flutter 中

javascript - 检测虚拟MAC地址?