C#trayicon使用wpf

Log*_*gan 11 c# wpf trayicon

虽然我已经在unity3D中编写了C#几年,但我对C#编程非常陌生.我正在尝试制作一个WPF托盘图标,我在网上找到的所有资源都告诉我使用

System.Windows.Forms
Run Code Online (Sandbox Code Playgroud)

但是.对于我来说,System.Windows中没有.Forms,我不知道为什么不这样做.谁能帮我这个?

Err*_*Efe 30

您需要添加对System.Window.Forms和System.Drawing程序集的引用,然后像这样使用它.假设您尝试最小化Window to tray图标并在用户单击该图标时再次显示它:

public partial class Window : System.Windows.Window
{

    public Window()
    {
        InitializeComponent();

        System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
        ni.Icon = new System.Drawing.Icon("Main.ico");
        ni.Visible = true;
        ni.DoubleClick += 
            delegate(object sender, EventArgs args)
            {
                this.Show();
                this.WindowState = WindowState.Normal;
            };
    }

    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == WindowState.Minimized)
            this.Hide();

        base.OnStateChanged(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果你在一个带有System.Windows引用的窗口中得到它,你将遇到一些歧义问题.我通过为using添加一个名称来解决它:使用WinForms = System.Windows.Forms; 然后使用WinForms.NotifyIcon调用它notifyIcon = new WinForms.NotifyIcon(); (2认同)