我是c#的新手,并尝试编写w / o与
代码
我正在尝试构建一个系统托盘应用程序,它将在运行时更改notifyIcon。
我阅读了一些关于该主题的教程,并编写了以下代码,但我收到了这些错误,并且无法绕过它:
new 2.cs(41,9):错误CS0103:名称' notifyIcon1'在当前的情境中不存在 新的2.cs(41,37):错误CS0026:关键字'这个'在静态属性,静态方法或静态字段初始值设定项
中无效
任何人都可以帮助我吗? (这是我的代码的缩减版本...试图隔离问题。)
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");
}
}
}
答案 0 :(得分:0)
我认为你的notifyIcon1中的问题点击事件处理程序。将代码更改为下面的示例。
private static void notifyIcon1_Click(object Sender, EventArgs e)
{
((NotifyIcon)Sender).Icon = new Icon(Sender.GetType(), "Off.ico");
//notifyIcon1.Icon =
}