5

I am newbie c# , my first try on Visual Studio Code is to show notification on windows 10 using ToastContentBuilder from Namespace Microsoft.Toolkit.Uwp.Notifications here is my code :

using Microsoft.Toolkit.Uwp.Notifications;

namespace cs
{
class Program
{
    static void Main(string[] args)
    {
     
        new ToastContentBuilder ()
        .AddArgument("action","hello")
        .AddText("my first try in csharp)")
        .Show();
       
    }
}

}

and this is the error :'ToastContentBuilder' does not contain a definition for 'Show' and no accessible extension method 'Show' accepting a first argument of type 'ToastContentBuilder' could be found (are you missing a using directive or an assembly reference?)

mohammad ismael
  • 167
  • 5
  • 13

3 Answers3

4

Show() is only available with the #if WINDOWS_UWP conditional compilation symbol. See the source:

https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.Notifications/Toasts/Builder/ToastContentBuilder.cs

Try using a UWP project template instead of console app.

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
4

This now works if you set the TargetFramework to a higher version - the Microsoft documentation says Set your TFM to net5.0-windows10.0.17763.0, although I have only tested with net6.0-windows10.0.20348.0

See https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast?tabs=uwp#step-1-install-nuget-package.

Adam Baxter
  • 1,907
  • 21
  • 41
1

There is also a Community Toolkit to let you send toast notifications on Windows via code. Doc says it supports all C# app types, including WPF, UWP, WinForms, and Console: https://libraries.io/nuget/CommunityToolkit.WinUI.Notifications

Here is a program I put together today to create Windows Notifications from a commandline utility:

https://github.com/roblatour/notifyondemand

It is written in vb.net, and I used the .net framework 4.8.

Rob
  • 3,488
  • 3
  • 32
  • 27