NotifyIcon没有对其ContextMenu

时间:2017-05-21 05:02:36

标签: c# winforms contextmenu notifyicon

我的(无形系统托盘)程序有NotifyIcon,其ContextMenu值必须在程序的运行过程中动态更改。当程序启动时,通知图标对象将被实例化,如下所示:

notifyIcon = new NotifyIcon()
{
    Icon = new Icon(@"C:\Users\basil\documents\visual studio 2015\Projects\Project Repository\Project Repository\Program Icon.ico"),
    Text = "My Program Name",
    Visible = false // Don't make it visible yet!
};
notifyIcon.ContextMenu = new ContextMenu();
notifyIcon.ContextMenu.MenuItems.AddRange(new MenuItem[] { new MenuItem("Host Service") { Index = 0 }, new MenuItem("Backup Configurations", new EventHandler(delegate { bckConf.Show(); })) { Index = 1 }, new MenuItem("Network Info", new EventHandler(delegate { new form_NetworkInfo().Show(); })) { Index = 2 }, new MenuItem("About", new EventHandler(delegate { new form_AboutUs().Show(); })) { Index = 3 }, new MenuItem("Exit", new EventHandler(delegate { TerminateProgram(ProgramTerminateReason.UserRequest); })) { Index = 4 } }); 

为了更改ContextMenu的{​​{1}},创建名为NotifyIcon的公共方法,因为菜单项需要根据其他类的对象生成的结果进行更改在程序中声明如下:(忽略" CurrentServiceState&#34的变量;数据类型并注意ChangeNotifyIconState对象被声明为类变量)

NotifyIcon

问题在于:当我致电static public void ChangeNotifyIconState(CurrentServiceState state) { notifyIcon.ContextMenu.MenuItems[0].MenuItems.Clear(); // First clear all menu items of the to-be-updated block if (state==CurrentServiceState.Running) { // Service has started running; disable the start button and activate the stop and restart buttons notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Start service", new EventHandler(delegate { HostServices.Start(); })) { Index = 0, Enabled = false }); notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Stop service", new EventHandler(delegate { HostServices.Stop(); })) { Index = 0, Enabled = true }); notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Restart service", new EventHandler(delegate { HostServices.Restart(); })) { Index = 0, Enabled = true }); } else if (state==CurrentServiceState.Stopped) { // Service has stopped running; disable the stop and restart button and activate the start button notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Start service", new EventHandler(delegate { HostServices.Start(); })) { Index = 0, Enabled = true }); notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Stop service", new EventHandler(delegate { HostServices.Stop(); })) { Index = 0, Enabled = false }); notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Restart service", new EventHandler(delegate { HostServices.Restart(); })) { Index = 0, Enabled = false })}; } } 时,ChangeNotifyIconState(CurrentServiceState.Running)所需的MenuItems不会更改/更新。有人可以帮助我摆脱这种意想不到的行为并向我解释其背后的原因吗?

提前致谢。

编辑:

我甚至将ContextMenu对象标记为易失性对象,因为我推测这是因为编译器缓存。事实证明,使对象NotifyIcon无法解决手头的问题并非如此。

1 个答案:

答案 0 :(得分:1)

以下是如何使用NotifyIcon创建没有表单的应用程序以及如何更改menuContextItems的{​​{1}}例如点击事件的示例:

NotifyIcon

以下是截图:

在应用程序启动时,将出现notifyIcon,菜单将如下所示: enter image description here

由于我的条件是using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp5 { static class Program { private static Random rd = new Random(); private static NotifyIcon notifyIcon; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); NotifyIcon ni = new NotifyIcon(new System.ComponentModel.Container()); ContextMenu cm = new ContextMenu(); cm.MenuItems.Add(new MenuItem("First", Click)); cm.MenuItems.Add(new MenuItem("Second", Click)); cm.MenuItems.Add(new MenuItem("Third", Click)); cm.MenuItems.Add(new MenuItem("Exit", (obj,e)=> { Application.Exit(); })); ni.Icon = new Icon(@"c:\Users\Admin\Desktop\img.ico"); ni.Text = "Form1 (NotifyIcon example)"; ni.ContextMenu = cm; ni.Visible = true; notifyIcon = ni; Application.Run(); } public static bool SomeCondition() { return (rd.Next(0, 9999) % 2) == 1; } public static void Click(object sender, EventArgs e) { if (SomeCondition()) { ContextMenu cm = new ContextMenu(); cm.MenuItems.Add(new MenuItem("Fourth", Click)); cm.MenuItems.Add(new MenuItem("Fifth", Click)); cm.MenuItems.Add(new MenuItem("Sixth", Click)); cm.MenuItems.Add(new MenuItem("Exit", (obj, args) => { Application.Exit(); })); notifyIcon.ContextMenu = cm; } else { ContextMenu cm = new ContextMenu(); cm.MenuItems.Add(new MenuItem("Third", Click)); cm.MenuItems.Add(new MenuItem("Sixth", Click)); cm.MenuItems.Add(new MenuItem("Fourth", Click)); cm.MenuItems.Add(new MenuItem("Exit", (obj, args) => { Application.Exit(); })); notifyIcon.ContextMenu = cm; } } } } truefalseclick任意menuItem,但&#34;退出&#34;将有效Click event handler并更改menucontext。因此,您将获得如下菜单: enter image description here

enter image description here