Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试创建类Toast通知时,无法多次实例化它_C#_Wpf_Class_Notifications_Toast - Fatal编程技术网

C# 尝试创建类Toast通知时,无法多次实例化它

C# 尝试创建类Toast通知时,无法多次实例化它,c#,wpf,class,notifications,toast,C#,Wpf,Class,Notifications,Toast,我试图在WPF中创建一个Toast通知,我的问题是,如果我在本例中太快地从数据库中删除了一些内容,那么当前的通知将使用新文本升级,而不是创建类的新实例,因此我可以看到两个通知 通知从主窗口变为StackPanel的子窗口,这样它始终具有相同的位置。这就是为什么我一开始就清除了孩子们 这个类可以工作,只是我不能一次处理多个通知 我错过了什么?对不起,我是一个初学者,我试着自己做这件事 这是我的班级代码 { public class NotificationToast {

我试图在WPF中创建一个Toast通知,我的问题是,如果我在本例中太快地从数据库中删除了一些内容,那么当前的通知将使用新文本升级,而不是创建类的新实例,因此我可以看到两个通知

通知从主窗口变为StackPanel的子窗口,这样它始终具有相同的位置。这就是为什么我一开始就清除了孩子们

这个类可以工作,只是我不能一次处理多个通知

我错过了什么?对不起,我是一个初学者,我试着自己做这件事

这是我的班级代码

{
   public class NotificationToast
    {
        public Border ToastBorder { get; set; }
        public Grid ToastGrid { get; set; }
        public TextBlock BodyBlock { get; set; }
        public String NotificationString { get; set; }
        public DoubleAnimation Animation1 { get; set; }
        public DoubleAnimation Animation2 { get; set; }
        public TranslateTransform Transformation1 { get; set; }
        public TranslateTransform Transformation2 { get; set; }

        public NotificationToast(MainWindow window, string notificationString)
        {
            InitializeWindow(window, notificationString);   
        }

        private void InitializeWindow(MainWindow window, string notificationString)
        {
            NotificationString = notificationString;
            ToastBorder = new Border();
            ToastBorder.Width = 250;
            ToastBorder.Height = 70;
            ToastBorder.BorderThickness = new Thickness(2);
            ToastBorder.BorderBrush = Brushes.IndianRed;
            ToastBorder.Background = new SolidColorBrush(Color.FromRgb(240, 143, 116));
            TextBlock BodyBlock = new TextBlock();
            BodyBlock.Width = 248;
            BodyBlock.Height = 68;
            BodyBlock.TextWrapping = TextWrapping.Wrap;
            BodyBlock.FontSize = 16;
            BodyBlock.Text = NotificationString;
            BodyBlock.Margin = new Thickness(5);

            ToastBorder.Child = BodyBlock;
            window.stackNotification.Children.Clear();
            window.stackNotification.Children.Add(ToastBorder);

            MovingIn(window.stackNotification);
            MovingOut(window.stackNotification);

        }

        private void MovingIn(StackPanel movingBorder)
        {
            TranslateTransform trans = new TranslateTransform();
            movingBorder.RenderTransform = trans;
            Animation1 = new DoubleAnimation(80, 0, TimeSpan.FromSeconds(1));
            trans.BeginAnimation(TranslateTransform.YProperty, Animation1);
        }
        private async void MovingOut(StackPanel movingBorder)
        {
            await Task.Delay(2500);

            TranslateTransform trans = new TranslateTransform();
            movingBorder.RenderTransform = trans;
            Animation2 = new DoubleAnimation(0, 80, TimeSpan.FromSeconds(1));
            trans.BeginAnimation(TranslateTransform.YProperty, Animation2);
        }
    }
}
然后我这样称呼这个班

WindowToast = new NotificationToast(ParentWindow, Convert.ToString("The Player " + PersonDetails.FirstName + " " + PersonDetails.LastName + " details has been updated."));

下面是一个使用编程模式的Toast消息的简短示例

Disclamer:我是自己从头开始做的,我不是一个专业的程序员。WPF是我的爱好,不是工作。因此,该解决方案经过测试,但可能包含错误或不准确的实现。别相信我

由于模式方法,我们需要两个助手类

//INPC接口实现,用于在某些属性发生更改时通知UI。
公共类NotifyPropertyChanged:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
=>PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
//需要易于使用的命令
公共类中继命令:ICommand
{
私有只读操作\u执行;
私有只读功能可执行;
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
公共RelayCommand(执行操作,Func canExecute=null)
{
_执行=执行;
_canExecute=canExecute;
}
公共布尔CanExecute(对象参数)=>_CanExecute==null | | | u CanExecute(参数);
public void Execute(对象参数)=>\u Execute(参数);
}
解决方案的功能:

  • 在右下角显示敬酒信息
  • 支持3种严重性:信息、警告、错误。信息的颜色取决于它
  • 支持调整一次显示的最大消息数
  • 排队接收超过限制的消息并稍后显示
  • 用户可以立即关闭任何消息
  • 每条信息在约10秒后消失
  • 添加了一些外观和消失动画
注意:

  • 不支持并发,例如从不同线程/任务推送消息
  • 不是用户控件或独立解决方案
…但你可以改进它。:)

数据类:

public enum ToastMessageSeverity
{
Info=0,
警告=1,
误差=2
}
公共课堂演讲信息
{
公共字符串消息{get;set;}
公共ToastMessageSeverity严重性{get;set;}
}
ToastView模型

public类ToastViewModel:ReadOnlyObservableCollection
{
私有只读可观察收集项目;
私有只读int_maxCount;
专用只读队列_messagesque;
私人ICommand _removietem;
私有void RemoveMessage(ToastMessage消息)
{
如果(_items.Contains(message))
{
_项目。删除(消息);
如果(_messagesque.Count>0)推送(_messagesque.Dequeue());
}
}
公共无效推送(ToastMessage消息)
{
如果(\u items.Count>=\u maxCount)
_messagesQueue.Enqueue(message);
其他的
{
_添加(消息);
Task.Run(async()=>wait Task.Delay(10500)).ContinueWith(=>RemoveMessage(message),TaskScheduler.FromCurrentSynchronizationContext());
}
}
public ICommand removietem=>\u removietem???(\u removietem=new RelayCommand(参数=>
{
if(参数为ToastMessage message)RemoveMessage(message);
}));
public-ToastViewModel():这(6){}
public-ToastViewModel(int-maxCount):这个(新的observeCollection(),maxCount){}
私有ToastViewModel(ObservableCollection项,int maxCount):基本(项)
{
_项目=项目;
_maxCount=maxCount;
_messagesQueue=新队列();
}
}
主视图模型

public类主视图模型:NotifyPropertyChanged
{
私人ToastViewModel_toastMessages;
专用ICommand_pushToast命令;
公共ToastViewModelToastMessages
{
获取=>\u演讲信息;
设置
{
_演讲信息=价值;
OnPropertyChanged();
}
}
专用整数计数器=0;
public ICommand PushToastCommand=>\u PushToastCommand??(\u PushToastCommand=new RelayCommand(参数=>
{
ToastMessageSeverity严重性=ToastMessageSeverity.Info;
if(参数为字符串severityString)
{
foreach(Enum.GetValues中的ToastMessageSeverity tms(typeof(ToastMessageSeverity)))
{
if(severityString==tms.ToString())
{
严重程度=tms;
打破
}
}
}
推送(新的ToastMessage{Message=severity+“Message”+counter++,severity=severity});
}));
公共主视图模型()
{
ToastMessages=新的ToastViewModel();
}
}
和完整标记(将允许复制整个应用程序)