Call an ICommand from Windows.Forms.NotifyIcon Event .MouseC



我创建了一个从Window Forms继承的自定义NotifyIcon对象。在这里查找NotifyIcon类的属性和方法时,我发现当用户从任务栏单击NotifyIcon时,我可以触发事件。因此,我在ViewModel 中创建了以下代码

namespace TestEnvironment
{
public class MainWindowViewModel : INotifyPropertyChanged
{
public ICommand ShowWindowsCommand //this is the ICommand I want to call in .MouseClick()
{
get { return new DelegateCommand<object>(FunctionShowWindows); }
}
private void FunctionShowWindows(object parameter)
{
ProgressBarTemplate ProgressBarInstance = (ProgressBarTemplate)Application.Current.Windows.OfType<Window>().SingleOrDefault(window => window.Name == "ProgressBarScreen");
if (ProgressBarInstance != null)
{
if (MainWindowInstance.MaxHeight > 725 & MainWindowInstance.MaxWidth > 1200)
{
MainWindowInstance.WindowState = WindowState.Maximized;
MainWindowInstance.Activate();
//MainWindowInstance.Topmost = true;
}
else
{
MainWindowInstance.WindowState = WindowState.Normal;
MainWindowInstance.Activate();
//MainWindowInstance.Topmost = true;
}
ProgressBarInstance.Show();
ProgressBarInstance.WindowState = WindowState.Normal;
//ProgressBarInstance.Topmost = true;
}
else
{
if (MainWindowInstance.MaxHeight > 725 & MainWindowInstance.MaxWidth > 1200)
{
MainWindowInstance.WindowState = WindowState.Maximized;
MainWindowInstance.Activate();
//MainWindowInstance.Topmost = true;
}
else
{
MainWindowInstance.WindowState = WindowState.Normal;
MainWindowInstance.Activate();
//MainWindowInstance.Topmost = true;
}
}
}
public ICommand RunCalculationCommand_Approach2
{
get { return new DelegateCommand<object>(ExecuteSqlAsync); }
}
private async void ExecuteSqlAsync(object obj)
{
Stream iconStream_one = Application.GetResourceStream(new Uri("pack://application:,,,/TestApp;component/Assets/loading.ico")).Stream;

System.Windows.Forms.NotifyIcon notification_object = new System.Windows.Forms.NotifyIcon
{
Icon = new Icon(iconStream_one),
Visible = true
};
// The complete Task API accepts a CancellationToken to allow cancellation.
try
{
DateTime timestamp_start = DateTime.Now;
await Task.Run(() => RunCalculationsMethod(object_progressbar, "LOG_DETAILS", 1, true, getconnectionstring, CatchErrorExceptionMessage, this.CancellationTokenSource.Token), this.CancellationTokenSource.Token);

string[] time_passed = DateTime.Now.Subtract(timestamp_start).ToString().Split(@":");
List<SucessfulCompletion> reportsucessfulcompletion = new List<SucessfulCompletion>();
reportsucessfulcompletion = CheckLogsFailSuccessProcedure(SQLServerConnectionDetails());
if (reportsucessfulcompletion[0].Result == 0)
{
notification_object.ShowBalloonTip(5000, "Hi", "This is a BallonTip from Windows Notification", System.Windows.Forms.ToolTipIcon.None);
//notification_object.MouseClick += new System.EventHandler(ShowWindowsCommand);
}
else
{
notification_object.ShowBalloonTip(5000, "Hi", "Hello World", System.Windows.Forms.ToolTipIcon.None);
//notification_object.MouseClick += new System.EventHandler(ShowWindowsCommand);
}
}
catch (Exception ex)
{
//..
}
finally
{
//..
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
Closing += CancelSqlOperationsOnClosing;
}
}
}

XAML

<Window x:Class="TestEnvironment.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestEnvironment"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Show Toast"
Padding="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Command="{Binding RunCalculationCommand_Approach2}"/>
</Grid>
</Window>

我想要的是从事件MouseClick调用ICommandShowWindowsCommand

//notification_object.MouseClick += new System.EventHandler(ShowWindowsCommand); -> This is what I want to fix

但是此事件接受EventHandlers。那么,有什么方法可以从NotifyIconMouseClick事件调用ICommand吗?

由于NotifyIcon实际上不是显示在页面上的控件,因此可以在ViewModel中实例化它。为了在单击图标时触发命令,只需实现对事件的回调。

MainWindowViewModel.cs

namespace TestEnvironment
{
public class MainWindowViewModel : INotifyPropertyChanged
{
public ICommand ShowWindowsCommand
{
get { return new DelegateCommand<object>(FunctionShowWindows); }
}
private void FunctionShowWindows(object parameter)
{
// redacted
}
public ICommand RunCalculationCommand_Approach2
{
get { return new DelegateCommand<object>(ExecuteSqlAsync); }
}
private async void ExecuteSqlAsync(object obj)
{
Stream iconStream_one = Application.GetResourceStream(new Uri("pack://application:,,,/TestApp;component/Assets/loading.ico")).Stream;

System.Windows.Forms.NotifyIcon notification_object = new System.Windows.Forms.NotifyIcon
{
Icon = new Icon(iconStream_one),
Visible = true
};
notification_object.MouseClick += new System.Windows.Forms.MouseEventHandler(NotifyIcon_MouseClick);
// redacted
}
private void NotifyIcon_MouseClick(object sender, EventArgs e);
{
FunctionShowWindows(null);
}
}
}

最新更新