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



如何在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行上得到一个错误。

错误是:

系统。NullReferenceException: 'Object reference not set to an Object instance .'

notifyIcon was Nothing.

你知道我错过了什么吗?事先感谢您的任何帮助。

放到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

最新更新