首页 > 解决方案 > #WPF ToastNotifications 返回 2 个异常

问题描述

我正在使用 ToastNotifcations 来处理通过 Service Broker 来自我的数据库的更新。

用途:

using TableDependency.SqlClient.Base.Enums;
using ToastNotifications;
using TableDependency.SqlClient;
using ToastNotifications.Lifetime;
using ToastNotifications.Messages;
using ToastNotifications.Position;
using System.Data.Entity.Core.EntityClient;

代码:

private void _dependency_OnChanged(object sender, TableDependency.SqlClient.Base.EventArgs.RecordChangedEventArgs<Aircraft> e)
        {
            Notifier notifier = new Notifier(cfg =>
            {
                cfg.PositionProvider = new WindowPositionProvider(
                    parentWindow: Application.Current.MainWindow,
                    corner: Corner.BottomRight,
                    offsetX: 10,
                    offsetY: 10);

                cfg.LifetimeSupervisor = new TimeAndCountBasedLifetimeSupervisor(
                    notificationLifetime: TimeSpan.FromSeconds(3),
                    maximumNotificationCount: MaximumNotificationCount.FromCount(5));

                cfg.Dispatcher = Application.Current.Dispatcher;
            });

            if (e.ChangeType != ChangeType.None)
            {
                switch (e.ChangeType)
                {
                    case ChangeType.Update:
                        e.Entity.ToString();

                        Console.WriteLine(e.Entity.ACStatus.ToString());
                                             
                        notifier.ShowWarning("I AM WORKING");
                        
                        break;
                }
            }
        }
        private void Notify_Closing(object sender, CancelEventArgs e)
        {
            _dependency.Stop();
        }

通过 Console.WriteLine 我得到了很好的结果,但是,notifier.ShowWarning将出现以下异常:

Exception thrown: 'System.InvalidOperationException' in WindowsBase.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll

有任何想法吗?

标签: sql-serverwpftoastsqldependency

解决方案


推荐阅读