简体   繁体   中英

How to receive textbox value from a Windows Toast Notificiation in a desktop app?

I'm writing a Windows 10 Desktop application in C# (not universal app, this is important) that needs to send a toast notification, which includes a textbox and a submit button.

I use ToastNotification.Activated to listen to when the submit button is clicked, but I can't find any way to extract the textbox text from the toast.

All the methods I've found - including MSFT article here and the StackOverflow answers given here , seem to apply to universal apps only, and I couldn't apply them to a desktop app.

Any clue on how to extract the textbox text input from a toast on a Win10 desktop app?

Attaching my code snippet below

Thanks

   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.";
        });
    }

In your App.xaml.cs file add an override void to handle 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM