如何观看和响应其他应用程序的Toast通知。 [英] How to watch and react on other application’s toast notification.
问题描述
Hello:
我有一些人的WPF应用程序在Windows 10操作中心显示简单的Toast通知。
以下是MainWindow.xaml:
<Window x:Class="DesktopToast.Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DesktopToast WPF Sample"
Width="320" Height="180"
x:Name="WindowRoot"
ResizeMode="NoResize" SizeToContent="Height"
Icon="Resources/Toast.ico">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
FontSize="14"
Content="Show a toast"
Click="Button_ShowToast_Click"/>
<Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
FontSize="14"
Content="Show an interactive toast"
IsEnabled="{Binding CanUseInteractiveToast, ElementName=WindowRoot, Mode=OneTime}"
Click="Button_ShowInteractiveToast_Click"/>
<Label Grid.Row="2" Grid.Column="0"
Content="ToastNotification"/>
<TextBox Grid.Row="2" Grid.Column="1"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Background="White"
IsReadOnly="True"
Text="{Binding ToastResult, ElementName=WindowRoot}"/>
<Label Grid.Row="3" Grid.Column="0"
Content="INotificationActivationCallback"/>
<TextBox Grid.Row="3" Grid.Column="1"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Background="White"
IsReadOnly="True"
Text="{Binding ActivationResult, ElementName=WindowRoot}"/>
<Label Grid.Row="4" Grid.Column="0"
Content="Message"/>
<TextBox Grid.Row="4" Grid.Column="1"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Background="White"
IsReadOnly="True"
Text="{Binding Message, ElementName=WindowRoot}"/>
</Grid>
</Window>
以下是MainWindow.xaml。 cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using NotificationsExtensions;
using NotificationsExtensions.Toasts;
namespace DesktopToast.Wpf
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// If "-Embedding" argument is appended, it will mean this application is started by COM.
if (Environment.GetCommandLineArgs().Last() == "-Embedding")
this.Title += " [COM]";
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// For Action Center of Windows 10
NotificationActivator.RegisterComType(typeof(NotificationActivator), OnActivated);
NotificationHelper.RegisterComServer(typeof(NotificationActivator), Assembly.GetExecutingAssembly().Location);
//NotificationHelper.UnregisterComServer(typeof(NotificationActivator));
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
// For Action Center of Windows 10
NotificationActivator.UnregisterComType();
}
private const string MessageId = "Message";
private void OnActivated(string arguments, Dictionary<string, string> data)
{
var result = "Activated";
if ((arguments?.StartsWith("action=")).GetValueOrDefault())
{
result = arguments.Substring("action=".Length);
if ((data?.ContainsKey(MessageId)).GetValueOrDefault())
Dispatcher.Invoke(() => Message = data[MessageId]);
}
Dispatcher.Invoke(() => ActivationResult = result);
}
#region Property
public string ToastResult
{
get { return (string)GetValue(ToastResultProperty); }
set { SetValue(ToastResultProperty, value); }
}
public static readonly DependencyProperty ToastResultProperty =
DependencyProperty.Register(
nameof(ToastResult),
typeof(string),
typeof(MainWindow),
new PropertyMetadata(string.Empty));
public string ActivationResult
{
get { return (string)GetValue(ActivationResultProperty); }
set { SetValue(ActivationResultProperty, value); }
}
public static readonly DependencyProperty ActivationResultProperty =
DependencyProperty.Register(
nameof(ActivationResult),
typeof(string),
typeof(MainWindow),
new PropertyMetadata(string.Empty));
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register(
nameof(Message),
typeof(string),
typeof(MainWindow),
new PropertyMetadata(string.Empty));
public bool CanUseInteractiveToast
{
get { return (bool)GetValue(CanUseInteractiveToastProperty); }
set { SetValue(CanUseInteractiveToastProperty, value); }
}
public static readonly DependencyProperty CanUseInteractiveToastProperty =
DependencyProperty.Register(
nameof(CanUseInteractiveToast),
typeof(bool),
typeof(MainWindow),
new PropertyMetadata(Environment.OSVersion.Version.Major >= 10));
#endregion
private void Clear()
{
ToastResult = "";
ActivationResult = "";
Message = "";
}
private async void Button_ShowToast_Click(object sender, RoutedEventArgs e)
{
Clear();
ToastResult = await ShowToastAsync();
}
private async Task<string> ShowToastAsync()
{
var request = new ToastRequest
{
ToastTitle = "DesktopToast WPF Sample",
ToastBody = "This is a toast test.",
ToastLogoFilePath = string.Format("file:///{0}", Path.GetFullPath("Resources/toast128.png")),
ShortcutFileName = "DesktopToast.Wpf.lnk",
ShortcutTargetFilePath = Assembly.GetExecutingAssembly().Location,
AppId = "DesktopToast.Wpf",
ActivatorId = typeof(NotificationActivator).GUID // For Action Center of Windows 10
};
var result = await ToastManager.ShowAsync(request);
return result.ToString();
}
private async void Button_ShowInteractiveToast_Click(object sender, RoutedEventArgs e)
{
Clear();
ToastResult = await ShowInteractiveToastAsync();
}
private async Task<string> ShowInteractiveToastAsync()
{
var request = new ToastRequest
{
ToastXml = ComposeInteractiveToast(),
ShortcutFileName = "DesktopToast.Wpf.lnk",
ShortcutTargetFilePath = Assembly.GetExecutingAssembly().Location,
AppId = "DesktopToast.Wpf",
ActivatorId = typeof(NotificationActivator).GUID
};
var result = await ToastManager.ShowAsync(request);
return result.ToString();
}
private string ComposeInteractiveToast()
{
var toastVisual = new ToastVisual
{
BindingGeneric = new ToastBindingGeneric
{
Children =
{
new AdaptiveText { Text = "DesktopToast WPF Sample" }, // Title
new AdaptiveText { Text = "This is an interactive toast test." }, // Body
},
AppLogoOverride = new ToastGenericAppLogo
{
Source = string.Format("file:///{0}", Path.GetFullPath("Resources/toast128.png")),
AlternateText = "Logo"
}
}
};
var toastAction = new ToastActionsCustom
{
Inputs =
{
new ToastTextBox(id: MessageId) { PlaceholderContent = "Input a message" }
},
Buttons =
{
new ToastButton(content: "Reply", arguments: "action=Replied") { ActivationType = ToastActivationType.Background },
new ToastButton(content: "Ignore", arguments: "action=Ignored")
}
};
var toastContent = new ToastContent
{
Visual = toastVisual,
Actions = toastAction,
Duration = ToastDuration.Long,
Audio = new NotificationsExtensions.Toasts.ToastAudio
{
Loop = true,
Src = new Uri("ms-winsoundevent:Notification.Looping.Alarm4")
}
};
return toastContent.GetContent();
}
}
}
代码作品。 但现在我要做以下事情:
首先,我想要知道我是否可以编写另一个WPF应用程序来收听这个
application 的吐司通知,我最好能读取吐司内容。
但我的最终目标是:我在Windows 10上的PC上运行了一些其他应用程序,它不时向Windows 10操作中心发送Toast通知,我可以看到它们靠我的眼睛;因为我没有
有这些其他应用程序的源代码,但我想编写一个程序,所以我可以对一个应用程序的toast通知作出反应;总共有大约20个这样的应用程序,我没有其中任何一个的源代码,但我有兴趣获得
并对一个应用程序的Toast通知作出反应。 我该怎么办? 请告知,即:我是否必须获得其他应用程序的GUID?或任何有用的信息,所以我可以尝试抓住它的吐司通知。
我的开发环境:Windows 10企业版,Visual Studio 2017版15.7.3
谢谢,
推荐答案
嗨zydjohn,
Hi zydjohn,
>>首先,我想知道是否可以编写另一个WPF应用程序来收听此应用程序的Toast通知,我最好能够阅读Toast内容。
>>First, I want to know if I can write another WPF application to listen to this application’s toast notification, it is better that I can read the toast content.
不,你不能,因为有属于不同的流程,据我所知,WPF应用程序不能与不同的流程社区。
No, you can't, because there are belong to different process, as far as I know, WPF application can't community with different process.
>>有t总共大约20个这样的应用程序,我没有其中任何一个的源代码,但我有兴趣获取并响应一个应用程序的Toast通知。 我该怎么办? 请告知,即:我有
来获取其他应用程序的GUID吗?
>>there are total about 20 such applications, I don’t have the source code for any of them, but I am interested in getting and reacting on one application’s toast notifications. What I should do? Please advise, i.e: do I have to get other application’s GUID?
我会写一个WCF应用程序,可以社区在20个应用程序中,所有20个应用程序都可以使用WCF应用程序。关于WPF消费wcf,请参考:
I would write a WCF application, which could community with the 20 applications, all of 20 applications can consume the WCF application. about WPF consuming wcf, please refer to:
https://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-wcf-service-in-wpf-application/
祝你好运,
张龙
这篇关于如何观看和响应其他应用程序的Toast通知。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!