【问题标题】:TaskbarIcon is created every time a button is clicked in WPF application每次在 WPF 应用程序中单击按钮时都会创建 TaskbarIcon
【发布时间】: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_calculations
  • tbi_calculation_status

第一个在等待Task.Run(() =&gt; RunCalculationsMethod(); 期间向用户显示,如我上面的代码所示。在任务计算过程中,用户可以点击TaskbarIcon,调用该函数ShowWindowsCommand。计算结束后,TaskbarIcon 对象tbi_during_calculations 连同其Icon 一起被释放。

然后生成第二个TaskBarIcon对象tbi_calculation_status,通知用户计算是否成功。根据计算,BalloonTip 会显示不同的消息。

我的问题是,当我得到我想要的东西时,我已经点击了应用程序。单个任务栏图标。但是,如果我在没有先关闭 WPF 应用程序的情况下再次单击该按钮,则会复制 TaskBarIcon 对象 tbi_calculation_status 而不会覆盖它的前一个实例。如下图所示,

你知道在不关闭应用程序的情况下摆脱第一个 TaskbarIcon 对象的聪明方法吗?

【问题讨论】:

    标签: c# wpf notification-icons


    【解决方案1】:

    您必须隐藏(处置)tbi_calculation_status 实例(与tbi_during_calculations 相同)

    每次单击按钮都会创建tbi_calculation_status 图标,因此您将在第二次按钮单击时获得两个tbi_calculation_status 图标(在第三次按钮单击时获得三个,等等)

    在一个应用程序的系统托盘中有两个图标有点奇怪。 您可以使用一个图标,但在计算开始/停止时调整工具提示。

    【讨论】:

    • 感谢您的回答。即使这是一个有效的答案,它也不是我想要选择的选项。这是因为,如果我处理第二个 TaskbarIcon,则显示的 BalloonTip 会在 5 秒后自动消失。所以在通知显示 5 秒,如果用户是 afk 那么他将永远不会收到通知。在不使用.Dispose() 选项的情况下还有其他选择吗?
    • @niksp 还有另一种选择。尝试只创建一次tbi_calculation_status(当显示表单时)并在表单关闭时处理它。您可以隐藏tbi_calculation_status 并在按钮点击事件中显示
    • 是的,这是一个选项。我想知道为什么 WPF 应用程序不覆盖 TaskBarIcon 的存在,如果已经有一个,而是创建一个副本。
    • tbi_calculation_status 的处理已经在应用程序退出结束时完成(如预期的那样)。但是,如果应用程序保持打开状态并且用户再次单击“运行”按钮,则旧的任务栏图标保持打开状态并创建一个新的。
    • @NikSp 尽量不要在按钮单击事件处理程序中创建tbi_calculation_status。如果它在应用程序出口处被释放,则在应用程序启动时创建它
    猜你喜欢
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2023-02-23
    • 1970-01-01
    相关资源
    最近更新 更多