C# 从Windows.Forms.NotifyIcon事件调用ICommand.MouseClick()[WPF应用程序]
我已经创建了一个从窗口窗体继承的自定义C# 从Windows.Forms.NotifyIcon事件调用ICommand.MouseClick()[WPF应用程序],c#,wpf,C#,Wpf,我已经创建了一个从窗口窗体继承的自定义NotifyIcon对象。在查找NotifyIcon类的属性和方法时,我发现当用户从任务栏单击NotifyIcon时,我可以触发一个事件。因此,我在ViewModel中创建了以下代码 命名空间测试环境 { 公共类MainWindowViewModel:INotifyPropertyChanged { public ICommand ShowWindowsCommand//这是我要调用的ICommand。MouseClick() { 获取{returnnewd
NotifyIcon
对象。在查找NotifyIcon类的属性和方法时,我发现当用户从任务栏单击NotifyIcon时,我可以触发一个事件。因此,我在ViewModel中创建了以下代码
命名空间测试环境
{
公共类MainWindowViewModel:INotifyPropertyChanged
{
public ICommand ShowWindowsCommand//这是我要调用的ICommand。MouseClick()
{
获取{returnnewdelegatecommand(FunctionShowWindows);}
}
私有void函数showwindows(对象参数)
{
ProgressBarTemplate ProgressBarInstance=(ProgressBarTemplate)Application.Current.Windows.OfType().SingleOrDefault(window=>window.Name==“ProgressBarScreen”);
if(ProgressBarInstance!=null)
{
如果(MainWindowInstance.MaxHeight>725和MainWindowInstance.MaxWidth>1200)
{
MainWindowInstance.WindowsState=WindowsState.Maximized;
MainWindowInstance.Activate();
//MainWindowInstance.Topmost=true;
}
其他的
{
MainWindowInstance.WindowsState=WindowsState.Normal;
MainWindowInstance.Activate();
//MainWindowInstance.Topmost=true;
}
ProgressBarInstance.Show();
ProgressBarInstance.WindowsState=WindowsState.Normal;
//ProgressBarInstance.Topmost=真;
}
其他的
{
如果(MainWindowInstance.MaxHeight>725和MainWindowInstance.MaxWidth>1200)
{
MainWindowInstance.WindowsState=WindowsState.Maximized;
MainWindowInstance.Activate();
//MainWindowInstance.Topmost=true;
}
其他的
{
MainWindowInstance.WindowsState=WindowsState.Normal;
MainWindowInstance.Activate();
//MainWindowInstance.Topmost=true;
}
}
}
公共ICommand RunCalculationCommand_方法2
{
获取{返回新的DelegateCommand(ExecuteSqlAsync);}
}
私有异步void ExecuteSqlAsync(对象obj)
{
Stream iconStream_one=Application.GetResourceStream(新Uri(“pack://application:,,,/TestApp;组件/资产/加载.ico”)。流;
System.Windows.Forms.NotifyIcon通知\u对象=新建System.Windows.Forms.NotifyIcon
{
Icon=新图标(iconStream_one),
可见=真
};
//complete Task API接受CancellationToken以允许取消。
尝试
{
DateTime timestamp_start=DateTime.Now;
等待Task.Run(()=>RunCalculationMethod(object_progressbar,“LOG_DETAILS”,1,true,getconnectionstring,CatchErrorExceptionMessage,this.CancellationTokenSource.Token),this.CancellationTokenSource.Token);
字符串[]time_passed=DateTime.Now.Subtract(timestamp_start.ToString().Split(@):);
List ReportSuccessfulCompletion=新建列表();
ReportSuccessfulCompletion=CheckLogsFailSuccessProcedure(SQLServerConnectionDetails());
如果(ReportSuccessfulCompletion[0]。结果==0)
{
notification_object.showBallootTip(5000,“您好”,“这是来自Windows通知的BallonTip”,System.Windows.Forms.ToolTipIcon.None);
//notification_object.MouseClick+=new System.EventHandler(ShowWindowsCommand);
}
其他的
{
通知对象.showBallootTip(5000,“你好”,“你好世界”,System.Windows.Forms.ToolTipIcon.None);
//notification_object.MouseClick+=new System.EventHandler(ShowWindowsCommand);
}
}
捕获(例外情况除外)
{
//..
}
最后
{
//..
}
}
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=新的MainWindowViewModel();
关闭+=取消SQLOperationsOnClose;
}
}
}
XAML
我想从事件鼠标单击
调用ICommandShowWindowsCommand
//通知\u object.MouseClick+=new System.EventHandler(ShowWindowsCommand);->这就是我想要解决的问题
但此事件接受EventHandler。那么,有没有办法从
NotifyIcon
的MouseClick
事件调用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);
}
}
}
命名空间测试环境
{
公共类MainWindowViewModel:INotifyPropertyChanged
{
公共ICommand ShowWindowsCommand
{
获取{returnnewdelegatecommand(FunctionShowWindows);}
}
私有void函数showwindows(对象参数)
{
//编辑
}
公共ICommand RunCalculationCommand_方法2
{
获取{返回新的DelegateCommand(ExecuteSqlAsync);}
}
私有异步void ExecuteSqlAsync(对象obj)
{
流iconStream_one=Application.GetRe