如何在xaml vb中设置最小化应用程序到系统通知托盘?

anhgbhbe  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(141)

如何在xaml vb中设置最小化应用程序到系统通知托盘?我想当我点击最小化应用程序图标时,它会自动将应用程序扔到系统托盘。
我在MainWindow.xaml中有这样的代码:

<Image x:Name="minimizeapp" Source="/minus.png" Cursor="Hand"/>

然后我在MainWindow.xaml.vb中有这样的代码:

Public Sub newInitialize()
   InitializeComponent()

   notifyIcon = New System.Windows.Forms.NotifyIcon()
   notifyIcon.Icon = New System.Drawing.Icon("./Resources/Chromatix-Aerial-Web.ico")
   notifyIcon.Visible = True
End Sub

Private Sub minimizeapp_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles minimizeapp.MouseDown
   Me.WindowState = WindowState.Minimized
   Me.Hide()
   notifyIcon.Visible = True
End Sub

当我单击minimizeapp图标时,我在notifiIcon.Visible = True行上得到一个错误。
错误是:
System.NullReferenceException:'对象引用未设置为对象的示例。'
notifyIcon是什么都没有。
你知道我错过了什么吗?提前感谢你的任何帮助。

c8ib6hqw

c8ib6hqw1#

将其放入Grid_Loaded中。

Private Sub Grid_Loaded(sender As Object, e As RoutedEventArgs)
    notifyicon = New NotifyIcon()
    notifyicon.Icon = New Icon("./Resources/Chromatix-Aerial-Web.ico")
    notifyicon.Visible = True
End Sub

Private Sub minimizeapp_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles minimizeapp.MouseDown
    Me.WindowState = WindowState.Minimized
    Me.Hide()
    notifyicon.Visible = True
End Sub

相关问题