如何在多个表单之间使用 1 个 notificationIcon?
我在项目的主窗体中添加了一个notifyIcon。我在项目中有其他表单,我希望能够使用 notificationIcon,尽管事实证明这很困难。在多个表单之间使用 1 notificationIcon 的最佳方法是什么?我读到了一篇关于不将其添加到表单中而是在其自己的类中实例化它的主题,这对我来说毫无意义。想法?
I have a notifyIcon added to my main form of a project. I have other forms in the project that I want to be able to use the notifyIcon though which is proving difficult. What would be the best way to use 1 notifyIcon between multiple forms? I read a thread about not adding it to a form but instantiating it in its own class which made no sense to me. Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需在主窗体上公开一个属性即可返回对 NotifyIcon 的引用。您甚至可以将其设为静态,因为只有一个:
其他类中的代码现在可以简单地使用 MainForm.Notifier。
Just expose a property on your main form to return a reference to the NotifyIcon. You can even make it static since there is only ever one:
Code in other classes can now simply use MainForm.Notifier.
在实现
NotifyIcon
的表单中创建一个公共静态变量:在表单中加载分配表单
NotifyIcon
:全部设置。现在您可以从项目中的任何位置访问通知图标
Create a public static variable in the Form where
NotifyIcon
is implemented:in form load assign forms
NotifyIcon
:all set. Now you can access the notify icon from anywhere in the project
即使您需要将
MainForm
分配给NotifyIcon
控件,也不会发现在不同窗体之间共享其对象有任何问题。让我们说得更清楚一点:
1.
MainForm
启动并初始化NotifyIcon
控件包装类,让我们调用NotifyControlHolder.
2.
NotifyControlHolder
类像公共属性一样存在于您的UIShared
中。3.
UIShared
单调类可以从应用程序的不同部分访问,可以绕过MainForm
访问它并更改其状态。Even if you need to assign
MainForm
toNotifyIcon
control, don't see any problem about sharing it's object between different forms.Let's say, to try to be more clear:
1.
MainForm
starts and initializesNotifyIcon
control wrapper class, let's callNotifyControlHolder
.2. That
NotifyControlHolder
class stands in yourUIShared
like a public property.3.
UIShared
singletone class is accesible from differnt parts of your application, which can access it and change it's state, bypassing theMainForm
.