我刚接触c#,尝试编写代码时不使用与。
我正在尝试构建一个系统托盘应用程序,它将在运行时更改notifyIcon。
我阅读了一些关于该主题的教程,并编写了以下代码,但我遇到了这些错误,无法解决:
new 2.cs(41,9):error CS0103:当前上下文中不存在名称“notifyIcon1”
new 2.cs(41,37):error CS0026:关键字“this”在静态属性、静态方法或静态字段初始值设定项中无效
有人能帮我吗?(这是我的代码的精简版……试图隔离问题。)
using System;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using System.Diagnostics;
namespace sysTrayApp
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
NotifyIcon notifyIcon1 = new NotifyIcon();
ContextMenu contextMenu1 = new ContextMenu();
MenuItem menuItem1 = new MenuItem();
contextMenu1.MenuItems.AddRange(new MenuItem[] { menuItem1 });
menuItem1.Index = 0;
menuItem1.Text = "Exit";
menuItem1.Click += new EventHandler(menuItem1_Click);
notifyIcon1.Icon = new Icon("On.ico");
notifyIcon1.Text = "some text";
notifyIcon1.ContextMenu = contextMenu1;
notifyIcon1.Click += new EventHandler(notifyIcon1_Click);
notifyIcon1.Visible = true;
Application.Run();
}
private static void menuItem1_Click(object Sender, System.EventArgs e)
{
Application.Exit();
}
private static void notifyIcon1_Click(object Sender, EventArgs e)
{
notifyIcon1.Icon = new Icon(this.GetType(),"Off.ico");
}
}
}