C#:在系统托盘中带有NotifyIcon的应用程序->;无法添加上下文菜单

C#:在系统托盘中带有NotifyIcon的应用程序->;无法添加上下文菜单,c#,forms,contextmenu,C#,Forms,Contextmenu,我只是从C#(Windows10/MSStudio 2019)开始。应用程序应具有否表单,并且在系统托盘中只有一个图标,带有上下文菜单: 我在图标上使用了以下代码&它可以工作: using System; using System.Collections.Generic; using System.Windows.Forms; using System.Diagnostics; using System.IO; namespace demo { static class Program

我只是从C#(Windows10/MSStudio 2019)开始。应用程序应具有表单,并且在系统托盘中只有一个图标,带有上下文菜单:

我在图标上使用了以下代码&它可以工作:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace demo
{
    static class Program
    {
    [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            NotifyIcon notifyIcon1 = new NotifyIcon();

            notifyIcon1.Icon = new System.Drawing.Icon(System.Windows.Forms.Application.StartupPath + @"\icon.ico");
            notifyIcon1.Text = "Demo";
            notifyIcon1.Visible = true;
            Application.Run();
         }
    }
}
 
但我无法向图标添加上下文菜单。代码示例自

ContextMenu contextMenu1 = new ContextMenu();
失败了。错误:“类型或名称空间名称上下文菜单不可用”

我认为is包含在“System.Windows.Forms”

有什么想法/解决方案吗

非常感谢
Andreas

您的windows窗体项目是否以net core 3.1或更高版本(.net 5)为目标?contextmenu的文档说明它在这种情况下不可用。