标签:
老端方,先看效果
右下角的notification:
操纵中心的notification:
整体效果:
前提条件
1.需要在开始菜单里添加快捷方法。
2.在注册内外注册你实现了INotificationActivationCallBack接口的com组件。
3.一个APP_ID,添加到快捷方法里,,ActionCenter会以此来区分差别应用的动静。
缺一不成,不然弹出的notification没法交互。
实现
1.添加快捷方法
private void InstallShortcut(String shortcutPath, String exePath)
{
IShellLinkW newShortcut = (IShellLinkW)new CShellLink()
;
newShortcut
.SetPath(exePath)
;
IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut
;
PropVariantHelper varAppId = new PropVariantHelper()
;
varAppId
.SetValue(APP_ID)
;
newShortcutProperties
.SetValue(PROPERTYKEY
.AppUserModel_ID, varAppId
.Propvariant)
;
PropVariantHelper varToastId = new PropVariantHelper()
;
varToastId
.VarType = VarEnum
.VT_CLSID
;
varToastId
.SetValue(typeof(NotificationActivator)
.GUID)
;
newShortcutProperties
.SetValue(PROPERTYKEY
.AppUserModel_ToastActivatorCLSID, varToastId
.Propvariant)
;
ShellHelpers
.IPersistFile newShortcutSave = (ShellHelpers
.IPersistFile)newShortcut
;
newShortcutSave
.Save(shortcutPath, true)
;
}
2.注册com组件
private void RegisterComServer(String exePath)
{
string regString = String.Format(
"SOFTWARE\\Classes\\CLSID\\{{{0}}}\\LocalServer32",
typeof(NotificationActivator).GUID);
var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(regString);
key.SetValue(
null, exePath);
}
这样ActionCenter就可以通过GUID找到你的exe文件。
3.设置通知的内容样式
通知的样式有很多种,图片、文字、按钮、输入框可以组合使用。详情见最下面的参考链接。
这里我贴出下我例子里的构造设置。
private void btncl(object sender, RoutedEventArgs e)
{
ToastContent content
= new ToastContent()
{
Visual
= new ToastVisual()
{
BindingGeneric
= new ToastBindingGeneric()
{
Children
=
{
new AdaptiveText()
{
Text
="New Mirrored Folders Created"//标题
},
new AdaptiveText()
{
Text
="Drag some files to either Mirror folder to sync\nClick to show the Mirror folder on my..."//内容
}
},
AppLogoOverride
= new ToastGenericAppLogo()
{
Source
= new System
.Uri(System
.IO
.Path
.GetFullPath(
"123.png"))
.AbsoluteUri
//通知的图标
}
}
},
Scenario
= ToastScenario
.Alarm,
//设置通知的声音
//三个button
Actions
= new ToastActionsCustom()
{
Buttons
=
{
new ToastButton(
"PC",
new QueryString(){
{
"action",
"fileExplorer" },
{
"path",
"c:\\" }
}
.ToString())
{
ActivationType
=ToastActivationType
.Background
},
new ToastButton(
"Drive",
new QueryString(){
{
"action",
"fileExplorer" },
{
"path",
"d:\\" }
}
.ToString())
{
ActivationType
=ToastActivationType
.Background
},
new ToastButtonDismiss(
"Close")
}
}
};
XmlDocument
xml = new XmlDocument();
xml.LoadXml(content
.GetContent());
ToastNotification toast
= new ToastNotification(
xml);
toast
.Group = "gg";
//toast.ExpirationTime = DateTime.Now.AddSeconds(20);
//toast.SuppressPopup = true;
ToastNotificationManager
.CreateToastNotifier(APP_ID)
.Show(toast);
}