5

はそれがToastNotificationManagerを使用して、コンソールアプリケーションからのトースト通知を送信することは可能ですか?コンソールアプリケーションからToast通知を送信することはできますか?

私はWindowsのユニバーサルアプリからトースト通知を送信することが可能であることを知っている:

var toast = new ToastNotification(doc); 
ToastNotificationManager.CreateToastNotifier().Show(toast); 

*ドキュメント - 私はWindows.UI.Notificationsが必要ToastNotificaionManagerを使用するには

XML列に格納トースト私はコンソールアプリケーションプロジェクトでは参照できません。

私は前にmentionetライブラリはactualy WinRTのに使用されます。 WinRT APIをWindowsコンソールアプリケーションで使用できますか?あなたのプログラムは、WinRTのライブラリ使用することを宣言する必要がまず

+0

コンソールアプリケーションにはGUIがないため、トーストは意味がありません。 – ManoDestra

+0

トーストは何ですか?あなた自身の質問に答えたようです。 –

+0

通知には多くのアプリケーションがあり、この場合は実際には本当に必要です –

答えて

11


1.右右あなたのyouProject(使用不可)をクリックして編集]をクリックしますプロジェクトのアンロード
2.を選択し、あなたのyourProjectをクリックしてくださいyourProject.csproj

3.新しいプロパティグループを追加します。<targetplatformversion>8.0</targetplatformversion>
4.リロードプロジェクト
5. のWindowsからreferece のWindowsを追加>コア
enter image description here

今、あなたは、このコードを追加する必要があります。

using Windows.UI.Notifications; 

、あなたはこのコードを使用して通知を送信することができるようになります:

var toast = new ToastNotification(doc); 
ToastNotificationManager.CreateToastNotifier().Show(toast); 

参考:How to call WinRT APIs in Windows 8 from C# Desktop Applications - WinRT Diagram

+0

編集後、コンソールアプリケーションが参照エラーで起動しない:ファイルまたはアセンブリを読み込めませんでした 'System.Runtime、Version = 4.0.20.0、Culture = neutral、PublicKeyToken = b03f5f7f11d50a3a' – Jamby

3

私はEvaldas Bのコードでいくつかの問題が発生しました。文字列がありませんでした。 (それはここでは文字列が必要です言うところ)

.CreateToastNotifier(<needed a string here>).Show(toast);

私は私のコードは、おそらくsucks-種類の新しいC#にしていますが、それは仕事をし、かなり単純化され、それは私がほとんどのために言うことができるよりも多くのです警告ソリューション私はまた、私は読むためのXML文書を取得し、時間の地獄を持っていた

を発見しました。私はSystem.xml(私が思う)とWindows.Data.Dom.Xml(また完全にはわからない)と戦っていた。 は最後に、私は私のサンプルファイルのためにそれらのハードコードされた文字列作ることに定住し、それらを切り替えるためにswitch文を使用していました。 私は、スタックオーバーフローの上に、出ている解決策を探して、人々のトンを発見しました。コンソールまたはバックグラウンドアプリケーションとトースト通知システムの使用は、スーパー有用であろうと思われる、とすべてのWindowsアプリケーションとのトースト通知システムを囲んでのドキュメントは、アプリケーションで使用する必要があることを示唆しています。アクションセンターは、NotificationTray/NotifyIconルートの通知に非常に便利です。 Web上の他の場所では完全なソリューションを見つけられませんでした。ここにコードの例を示します。

/* 
At first you need to declare that your program will be using winRT libraries: 
1. Right click on your yourProject, select Unload Project 
2. Right click on your youProject(unavailable) and click Edit yourProject.csproj 
3. Add a new property group:<TargetPlatformVersion>8.0</TargetPlatformVersion> 
4. Reload project 
5. Add referece Windows from Windows > Core 
*/ 
using System; 
using Windows.Data.Xml.Dom; 
using Windows.Storage; 
using Windows.Storage.Streams; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Windows.UI.Notifications; 

namespace ConsoleApplication6 
{ 
    public class NewToastNotification 
    { 
     public NewToastNotification(string input, int type) 
     { 
      string NotificationTextThing = input; 
      string Toast = ""; 
      switch (type) 
      { 
       case 1: 
        { 
         //Basic Toast 
         Toast = "<toast><visual><binding template=\"ToastImageAndText01\"><text id = \"1\" >"; 
         Toast += NotificationTextThing; 
         Toast += "</text></binding></visual></toast>"; 
         break; 
        } 
       default: 
        { 
         Toast = "<toast><visual><binding template=\"ToastImageAndText01\"><text id = \"1\" >"; 
         Toast += "Default Text String"; 
         Toast += "</text></binding></visual></toast>"; 
         break; 
        } 
      } 
      XmlDocument tileXml = new XmlDocument(); 
      tileXml.LoadXml(Toast); 
      var toast = new ToastNotification(tileXml); 
      ToastNotificationManager.CreateToastNotifier("New Toast Thing").Show(toast); 
     } 
} 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      NewToastNotification Window = new NewToastNotification("Yes",1); 


     } 
    } 
} 
+0

これに対する答え? CreateToastNotifier呼び出しに渡されるアプリケーションIDはどのようにする必要がありますか?そのアプリケーションIDを見つけるには?普遍的なアプリケーションのマニフェストを見ている可能性のあるさまざまなバリエーションを試しましたが、そのうちのどれもうまくいきませトーストは表示されますが、普段のアプリ内のボタンをクリックすると起動しません。実際に私のケースでは、普遍的なアプリケーションは、デスクトップブリッジの普遍的なアプリ、WPFはUWPのコンテナにラップされます。 – Alex

0

1)コンソールまたはデスクトップアプリケーションを使用してトースト通知を表示するには、アプリケーションにスタートメニューのショートカットが必要です。

2)アプリケーションがWindowsのスタートメニューにショートカットアイコン(タイルアイコンではない)を表示するには、AppIdが必要です。 ショートカットを作成するには、アプリケーションにShellHelpers.csという名前の新しいクラスを作成し、このコードを貼り付けます。ショートカットを作成するための

using System; 
using System.Runtime.InteropServices; 
using System.Text; 
using Microsoft.WindowsAPICodePack.Shell.PropertySystem; 
using MS.WindowsAPICodePack.Internal; 

namespace DesktopToastsSample.ShellHelpers 
{ 
    internal enum STGM : long 
    { 
     STGM_READ = 0x00000000L, 
    STGM_WRITE = 0x00000001L, 
    STGM_READWRITE = 0x00000002L, 
    STGM_SHARE_DENY_NONE = 0x00000040L, 
    STGM_SHARE_DENY_READ = 0x00000030L, 
    STGM_SHARE_DENY_WRITE = 0x00000020L, 
    STGM_SHARE_EXCLUSIVE = 0x00000010L, 
    STGM_PRIORITY = 0x00040000L, 
    STGM_CREATE = 0x00001000L, 
    STGM_CONVERT = 0x00020000L, 
    STGM_FAILIFTHERE = 0x00000000L, 
    STGM_DIRECT = 0x00000000L, 
    STGM_TRANSACTED = 0x00010000L, 
    STGM_NOSCRATCH = 0x00100000L, 
    STGM_NOSNAPSHOT = 0x00200000L, 
    STGM_SIMPLE = 0x08000000L, 
    STGM_DIRECT_SWMR = 0x00400000L, 
    STGM_DELETEONRELEASE = 0x04000000L, 
} 

internal static class ShellIIDGuid 
{ 
    internal const string IShellLinkW = "000214F9-0000-0000-C000-000000000046"; 
    internal const string CShellLink = "00021401-0000-0000-C000-000000000046"; 
    internal const string IPersistFile = "0000010b-0000-0000-C000-000000000046"; 
    internal const string IPropertyStore = "886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99"; 
} 

[ComImport, 
Guid(ShellIIDGuid.IShellLinkW), 
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
internal interface IShellLinkW 
{ 
    UInt32 GetPath(
     [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, 
     int cchMaxPath, 
     //ref _WIN32_FIND_DATAW pfd, 
     IntPtr pfd, 
     uint fFlags); 
    UInt32 GetIDList(out IntPtr ppidl); 
    UInt32 SetIDList(IntPtr pidl); 
    UInt32 GetDescription(
     [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, 
     int cchMaxName); 
    UInt32 SetDescription(
     [MarshalAs(UnmanagedType.LPWStr)] string pszName); 
    UInt32 GetWorkingDirectory(
     [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, 
     int cchMaxPath 
     ); 
    UInt32 SetWorkingDirectory(
     [MarshalAs(UnmanagedType.LPWStr)] string pszDir); 
    UInt32 GetArguments(
     [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, 
     int cchMaxPath); 
    UInt32 SetArguments(
     [MarshalAs(UnmanagedType.LPWStr)] string pszArgs); 
    UInt32 GetHotKey(out short wHotKey); 
    UInt32 SetHotKey(short wHotKey); 
    UInt32 GetShowCmd(out uint iShowCmd); 
    UInt32 SetShowCmd(uint iShowCmd); 
    UInt32 GetIconLocation(
     [Out(), MarshalAs(UnmanagedType.LPWStr)] out StringBuilder pszIconPath, 
     int cchIconPath, 
     out int iIcon); 
    UInt32 SetIconLocation(
     [MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, 
     int iIcon); 
    UInt32 SetRelativePath(
     [MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, 
     uint dwReserved); 
    UInt32 Resolve(IntPtr hwnd, uint fFlags); 
    UInt32 SetPath(
     [MarshalAs(UnmanagedType.LPWStr)] string pszFile); 
} 

[ComImport, 
Guid(ShellIIDGuid.IPersistFile), 
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
internal interface IPersistFile 
{ 
    UInt32 GetCurFile(
     [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile 
    ); 
    UInt32 IsDirty(); 
    UInt32 Load(
     [MarshalAs(UnmanagedType.LPWStr)] string pszFileName, 
     [MarshalAs(UnmanagedType.U4)] STGM dwMode); 
    UInt32 Save(
     [MarshalAs(UnmanagedType.LPWStr)] string pszFileName, 
     bool fRemember); 
    UInt32 SaveCompleted(
     [MarshalAs(UnmanagedType.LPWStr)] string pszFileName); 
} 
[ComImport] 
[Guid(ShellIIDGuid.IPropertyStore)] 
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
interface IPropertyStore 
{ 
    UInt32 GetCount([Out] out uint propertyCount); 
    UInt32 GetAt([In] uint propertyIndex, out PropertyKey key); 
    UInt32 GetValue([In] ref PropertyKey key, [Out] PropVariant pv); 
    UInt32 SetValue([In] ref PropertyKey key, [In] PropVariant pv); 
    UInt32 Commit(); 
} 


[ComImport, 
Guid(ShellIIDGuid.CShellLink), 
ClassInterface(ClassInterfaceType.None)] 
internal class CShellLink { } 

public static class ErrorHelper 
{ 
    public static void VerifySucceeded(UInt32 hresult) 
    { 
     if (hresult > 1) 
     { 
      throw new Exception("Failed with HRESULT: " + hresult.ToString("X")); 
     } 
    } 
} 
} 

コード(このコードは、あなたがトーストを表示していますが、同じクラスに追加することができます)

public bool TryCreateShortcut() 
    { 
     String shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Windows\\Start Menu\\Programs\\FixSus Toasts Sample .lnk"; 
     if (!File.Exists(shortcutPath)) 
     { 
      InstallShortcut(shortcutPath); 
      return true; 
     } 
     return false; 
    } 

    private void InstallShortcut(String shortcutPath) 
    { 
     // Find the path to the current executable 
     String exePath = Process.GetCurrentProcess().MainModule.FileName; 
     IShellLinkW newShortcut = (IShellLinkW)new CShellLink(); 

     // Create a shortcut to the exe 
     DesktopToastsSample.ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath)); 
     DesktopToastsSample.ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetArguments("")); 

     // Open the shortcut property store, set the AppUserModelId property 
     IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; 

     using (PropVariant appId = new PropVariant(APP_ID)) 
     { 
      DesktopToastsSample.ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId)); 
      DesktopToastsSample.ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); 
     } 

     // Commit the shortcut to disk 
     IPersistFile newShortcutSave = (IPersistFile)newShortcut; 

     DesktopToastsSample.ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); 
    } 

今、あなたはトースト

// Get a toast XML template 
     XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04); 

     // Fill in the text elements 
     XmlNodeList stringElements = toastXml.GetElementsByTagName("text"); 
     stringElements[1].AppendChild(toastXml.CreateTextNode("Message" + newMessage)); 


     // Specify the absolute path to an image 
     string codeWebFolderPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\")); 
     String imagePath = "file:///" + Path.GetFullPath(codeWebFolderPath+ "Resources\\FixSus.png"); 
     XmlNodeList imageElements = toastXml.GetElementsByTagName("image"); 
     imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath; 

     // Create the toast and attach event listeners 
     ToastNotification toast = new ToastNotification(toastXml); 

     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); 
ショーを作成することができます

APP_IDには任意の文字列を使用できます。私の場合、それは "NotificationTest.KEY"でした。 注:ShellHelperクラスは変更しないでください。 編集:最初にEvaldas Bの答えに従ってから、この解決策を適用してください。

関連する問題