如何将文本节点添加到Toast通知

时间:2015-08-19 20:44:50

标签: delphi delphi-xe8

这个Toast通知在桌面上使用XE8和Windows 10非常有效,但我无法弄清楚如何在通知中添加文本行。显示iTitle但不显示iMessage。这对我来说是全新的,所以我不知道要追求哪个方向。

第五次编辑....

Remy的优秀演示吐司程序是对原始Embaracdero代码的一个重大改进,但我不认为Remy实际上测试了代码,因为它不会像编写的那样编译。我不得不将TWindowString更改为TWindowsString,将IXmlNode更改为Xml_Dom_IXmlNode以使其进行编译。

以下实际编译但它在GetActivationFactory函数中生成访问冲突。

如果我们能够正常运行,那么它将比原来的Embarcadero代码有了很大的改进,对其他开发者来说应该是有价值的。

 procedure TForm1.ShowToast(const AMessage: String; const ATitle: String = '');
{ Send a Toast Notification }
var
  LINotificationManagerStatics: IToastNotificationManagerStatics;
  LToast: IToastNotification;
  LToastFactory: IToastNotificationFactory;
  LToastNotifier: IToastNotifier;
  LToastTemplateType: ToastTemplateType;
  LAccepted: TAcceptedEventHandler;
  LXMLTemplate: Xml_Dom_IXmlDocument;
  iTextNode: Xml_Dom_IXmlNode;
  LTextNodeList: Xml_Dom_IXmlNodeList;

  function GetActivationFactory(const ClassId: String; const Iid: String): IInspectable;
  begin
    OleCheck(RoGetActivationFactory(TWindowsString(ClassId), TGUID.Create(Iid), Result));
   // This produces an access violation at run time
  end;

begin
  LINotificationManagerStatics := GetActivationFactory(SToastNotificationManager, '{50AC103F-D235-4598-BBEF-98FE4D1A3AD4}') as IToastNotificationManagerStatics;
  LToastNotifier := LINotificationManagerStatics.CreateToastNotifier(TWindowsString(Edit1.Text));
  if ATitle <> '' then begin
    LToastTemplateType := ToastTemplateType.ToastText02;
  end else begin
    LToastTemplateType := ToastTemplateType.ToastText01;
  end;
  LXMLTemplate := LINotificationManagerStatics.GetTemplateContent(LToastTemplateType);
  LTextNodeList := LXMLTemplate.getElementsByTagName(TWindowsString('text'));
  if ATitle <> '' then
  begin
    LTextNodeList.Item(0).AppendChild(LXMLTemplate.CreateTextNode(TWindowsString(ATitle)) as Xml_Dom_IXmlNode);
    iTextNode := LTextNodeList.Item(1);
  end else begin
    iTextNode := LTextNodeList.Item(0);
  end;
  iTextNode.AppendChild(LXMLTemplate.CreateTextNode(TWindowsString(AMessage)) as Xml_Dom_IXmlNode);
  LToastFactory := GetActivationFactory(SToastNotification, '{04124B20-82C6-4229-B109-FD9ED4662B53}') as IToastNotificationFactory;
  LToast := LToastFactory.CreateToastNotification(LXMLTemplate);
  LAccepted := TAcceptedEventHandler.Create;
  LToast.add_Activated(LAccepted);
  LToastNotifier.Show(LToast);
end;

2 个答案:

答案 0 :(得分:2)

IXmlDocument.CreateTextNode()创建并返回一个新的文本节点,但不要将其添加到XML文档中。你必须单独添加它。甚至在Toast文件中也证明了这一点:

Quickstart: Sending a toast notification (HTML)

Quickstart: Sending a toast notification (XAML)

例如:

var
  ...
  LTagName: HString;

...    
if Succeeded(WindowsCreateString(PWideChar(iMessage), Length(iMessage), LString3)) then
try
  if Succeeded(WindowsCreateString(PWideChar('text'), 4, LTagName)) then
  try
    LXMLTemplate.getElementsByTagName(LTagName).Item(0).AppendChild(LXMLTemplate.CreateTextNode(LString3) as IXmlNode);
    ...
  finally
    WindowsDeleteString(LTagName);
  end;
  ...
finally
  WindowsDeleteString(LString3);
end;
...

或者,使用IXmlNode.InnerText属性而不是IXmlDocument.CreateTextNode()方法:

LXMLTemplate.getElementsByTagName(LTagName).Item(0).InnerText := LString3;

坦率地说,你基于代码的Embarcadero's example有点乱。它可以使用一些严肃的清理。

尝试更像这样的事情:

uses
  ...,
  System.SysUtils,
  System.Win.ComObj,
  Winapi.Data,
  System.WinrtHelpers; // see https://github.com/tgerdes/DelphiWinRT/blob/master/System.WinrtHelpers.pas

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowToast('The recycle bin is empty', 'Recycle Bin Is Empty');
end;

procedure TForm1.ShowToast(const AMessage: String; const ATitle: String = '');
{ Send a Toast Notification }
var
  LINotificationManagerStatics: IToastNotificationManagerStatics;
  LToast: IToastNotification;
  LToastFactory: IToastNotificationFactory;
  LToastNotifier: IToastNotifier;
  LToastTemplateType: ToastTemplateType;
  LAccepted: TAcceptedEventHandler;
  LXMLTemplate: Xml_Dom_IXmlDocument;
  iTextNode: Xml_Dom_IXmlNode;
  LTextNodeList: Xml_Dom_IXmlNodeList;

  function GetActivationFactory(const ClassId: String; const Iid: String): IInspectable;
  begin
    OleCheck(RoGetActivationFactory(TWindowsString(ClassId), TGUID.Create(Iid), Result));
  end;

begin
  LINotificationManagerStatics := GetActivationFactory(SToastNotificationManager, '{50AC103F-D235-4598-BBEF-98FE4D1A3AD4}') as IToastNotificationManagerStatics;
  LToastNotifier := LINotificationManagerStatics.CreateToastNotifier(TWindowString(Edit1.Text));
  if ATitle <> '' then begin
    LToastTemplateType := ToastTemplateType.ToastText02;
  end else begin
    LToastTemplateType := ToastTemplateType.ToastText01;
  end;
  LXMLTemplate := LINotificationManagerStatics.GetTemplateContent(LToastTemplateType);
  LTextNodeList := LXMLTemplate.getElementsByTagName(TWindowString('text'));
  if ATitle <> '' then
  begin
    LTextNodeList.Item(0).AppendChild(LXMLTemplate.CreateTextNode(TWindowString(ATitle)) as IXmlNode);
    iTextNode := LTextNodeList.Item(1);
  end else begin
    iTextNode := LTextNodeList.Item(0);
  end;
  iTextNode.AppendChild(LXMLTemplate.CreateTextNode(TWindowString(AMessage)) as IXmlNode);
  LToastFactory := GetActivationFactory(SToastNotification, '{04124B20-82C6-4229-B109-FD9ED4662B53}') as IToastNotificationFactory;
  LToast := LToastFactory.CreateToastNotification(LXMLTemplate);
  LAccepted := TAcceptedEventHandler.Create;
  LToast.add_Activated(LAccepted);
  LToastNotifier.Show(LToast);
end;

答案 1 :(得分:0)

经过大量调试后,我终于成功获得Toast Notification以显示标题和消息。事实证明,我必须使用Remy的一个修改版本的答案,结合一些Embaracdero的原始演示代码才能使其发挥作用。

谢谢你,雷米!我不认为我会去那里,但我终于做到了。 为了拯救那些试图做到这一点的人,我的工作代码如下所示:

procedure TForm1.ShowToast(const AMessage: String; const ATitle: String = '');
{ Send a Toast Notification }
var
  LINotificationManagerStatics: IToastNotificationManagerStatics;
  LToast: IToastNotification;
  LToastFactory: IToastNotificationFactory;
  LToastNotifier: IToastNotifier;
  LClassId: HString;
  LAccepted: TAcceptedEventHandler;
  LXMLTemplate: Xml_Dom_IXmlDocument;
  iTextNode: Xml_Dom_IXmlNode;
  LTextNodeList: Xml_Dom_IXmlNodeList;
  LTagName: HString;
  LTitle: HString;
  LMessage: HString;

  function GetActivationFactory(const ClassId: String; const Iid: String)
    : IInspectable;
  begin
    if Succeeded(WindowsCreateString(PWideChar(ClassId), Length(ClassId),
      LClassId)) then
      OleCheck(RoGetActivationFactory(LClassId, TGUID.Create(Iid), Result));
  end;

begin
  LINotificationManagerStatics := GetActivationFactory
    (SToastNotificationManager, '{50AC103F-D235-4598-BBEF-98FE4D1A3AD4}')
    as IToastNotificationManagerStatics;
  if Succeeded(WindowsCreateString(PWideChar(NotificationTitle1.Text),
    Length(NotificationTitle1.Text), LClassId)) then
    LToastNotifier := LINotificationManagerStatics.CreateToastNotifier
      (LClassId);
  LXMLTemplate := LINotificationManagerStatics.GetTemplateContent
    (ToastTemplateType.ToastText02);
  if Succeeded(WindowsCreateString(PWideChar('text'), Length('text'), LTagName))
  then
    LTextNodeList := LXMLTemplate.getElementsByTagName(LTagName);
  if ATitle <> '' then
  begin
    if Succeeded(WindowsCreateString(PWideChar(ATitle), Length(ATitle), LTitle))
    then
      LTextNodeList.Item(0).AppendChild(LXMLTemplate.CreateTextNode(LTitle)
        as Xml_Dom_IXmlNode);
    iTextNode := LTextNodeList.Item(1);
  end
  else
  begin
    iTextNode := LTextNodeList.Item(0);
  end;
  if Succeeded(WindowsCreateString(PWideChar(AMessage), Length(AMessage),
    LMessage)) then
    iTextNode.AppendChild(LXMLTemplate.CreateTextNode(LMessage)
      as Xml_Dom_IXmlNode);
  LToastFactory := GetActivationFactory(SToastNotification,
    '{04124B20-82C6-4229-B109-FD9ED4662B53}') as IToastNotificationFactory;
  LToast := LToastFactory.CreateToastNotification(LXMLTemplate);
  LAccepted := TAcceptedEventHandler.Create;
  LToast.add_Activated(LAccepted);
  LToastNotifier.Show(LToast);
end;

<强>用法:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowToast(NotificationMessage1.Text, NotificationTitle1.Text);
end;