【发布时间】:2021-01-06 21:50:23
【问题描述】:
我创建了一个 wpf 应用程序,它有一个绑定到 ICommand 的按钮。单击按钮时,会创建两个 TaskbarIcon 对象以及两个 Toast 通知或 BalloonTips,由 Philipp Sumi 使用的 Hardcodet.NotifyIcon.Wpf Nuget 包命名。
ICommand 代码
public ICommand RunCalculationCommand_Approach2
{
get { return new DelegateCommand<object>(ExecuteSqlAsync); }
}
private async void ExecuteSqlAsync(object obj)
{
//1. TaskBar During Calculations (during calculations)
Stream iconStream_one = Application.GetResourceStream(new Uri("pack://application:,,,/TestApp;component/Assets/loading.ico")).Stream;
Stream iconStream_two = Application.GetResourceStream(new Uri("pack://application:,,,/TestApp;component/Assets/loading.ico")).Stream;
TaskbarIcon tbi_during_calculations = new TaskbarIcon
{
Icon = new Icon(iconStream_one),
LeftClickCommand = ShowWindowsCommand,
};
//2. TaskBar Calculations Status (calculations completed-failed-stopped)
TaskbarIcon tbi_calculation_status = new TaskbarIcon
{
Icon = new Icon(iconStream_two),
LeftClickCommand = ShowWindowsCommand,
};
try
{
//Notify the user that calculations have already started
tbi_during_calculations.Visibility = Visibility.Visible;
string title_startup = "Calculations Started";
string text_startup = "You can now minimize the window and return back once the calculations finish.";
tbi_during_calculations.ShowBalloonTip(title_startup, text_startup, BalloonIcon.None);
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(@":");
//The code below is executed after the Task is completed or if it's cancelled.
List<SucessfulCompletion> reportsucessfulcompletion = new List<SucessfulCompletion>();
reportsucessfulcompletion = CheckLogsFailSuccessProcedure(SQLServerConnectionDetails());
tbi_during_calculations.Icon.Dispose();
tbi_during_calculations.Dispose();
if (reportsucessfulcompletion[0].Result == 0)
{
//Add Balloon tip
tbi_calculation_status.Visibility = Visibility.Visible;
string title = "Calculations Completed";
string text = $"Calculations have been completed in \n{time_passed[0]} hour(s), {time_passed[1]} minute(s) and {time_passed[2].Substring(0, 2)} seconds";
tbi_calculation_status.ShowBalloonTip(title, text, BalloonIcon.None);
}
else
{
//Add Balloon tip
tbi_calculation_status.Visibility = Visibility.Visible;
string title = "Calculations Failed";
string text = $"Calculations stopped \n{time_passed[0]} hour(s), {time_passed[1]} minute(s) and {time_passed[2].Substring(0, 2)} seconds ago";
tbi_calculation_status.ShowBalloonTip(title, text, BalloonIcon.None);
}
}
catch (Exception ex)
{
//..
}
finally
{
win_progressbar.Close();
EnableRunCalculationsButton = true;
}
}
您会注意到,在上面的代码 sn-p 中,我生成了两个 TaskbarIcon 对象:
tbi_during_calculationstbi_calculation_status
第一个在等待Task.Run(() => RunCalculationsMethod(); 期间向用户显示,如我上面的代码所示。在任务计算过程中,用户可以点击TaskbarIcon,调用该函数ShowWindowsCommand。计算结束后,TaskbarIcon 对象tbi_during_calculations 连同其Icon 一起被释放。
然后生成第二个TaskBarIcon对象tbi_calculation_status,通知用户计算是否成功。根据计算,BalloonTip 会显示不同的消息。
我的问题是,当我得到我想要的东西时,我已经点击了应用程序。单个任务栏图标。但是,如果我在没有先关闭 WPF 应用程序的情况下再次单击该按钮,则会复制 TaskBarIcon 对象 tbi_calculation_status 而不会覆盖它的前一个实例。如下图所示,
你知道在不关闭应用程序的情况下摆脱第一个 TaskbarIcon 对象的聪明方法吗?
【问题讨论】:
标签: c# wpf notification-icons