ToastContentBuilder' does not contain a definition for 'Show'
Asked Answered
A

4

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?)

Adz answered 28/4, 2021 at 15:16 Comment(0)
A
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.

Arose answered 28/4, 2021 at 15:42 Comment(0)
V
6

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.

Vikkivikky answered 8/11, 2021 at 4:15 Comment(0)
A
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.

Arose answered 28/4, 2021 at 15:42 Comment(0)
S
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.

Satang answered 7/1, 2022 at 4:27 Comment(0)
J
1

Your TargetFramework must be set like this (earliest Windows 10 build):

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows10.0.17763.0</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
</ItemGroup>
Joaniejoann answered 2/7, 2024 at 14:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.