Enterwell / Wpf.Notifications

WPF notifications UI controls (as seen in VS Code)
MIT License
400 stars 40 forks source link

Make it disapear after n seconds? #3

Closed filipjelic closed 6 years ago

filipjelic commented 6 years ago

Is it possible to make it disappear after n seconds, implement like a small timer instead of just clicking buttons? Let's say I get a notification and if I don't click it for 5 seconds it automatically disappears...

AleksandarDev commented 6 years ago

Currently no such extension method is available if you're using LINQ builder, but you can try doing something like this:

var builder = this.Manager
    .CreateMessage()
    .Accent("#E0A030")
    .Background("#333")
    .HasBadge("Warn")
    .HasMessage("Failed to retrieve data.")
    .WithButton("Try again", button => { });

Task.Delay(5000).ContinueWith(ctx => this.Manager.Dismiss(builder.Message));

builder.Queue();
filipjelic commented 6 years ago

Okay, that will do, thanks for help...

ghost1372 commented 6 years ago

@AleksandarDev i tested your code but i get error: The calling thread cannot access this object because a different thread owns it.'

ghost1372 commented 6 years ago

this worked for me:

 builder.Queue();
                await Task.Delay(5000);
                this.Manager.Dismiss(builder.Message);
AleksandarDev commented 6 years ago

@ghost1372 Thanks for reporting the problem. That's probably because this.Manager.Dismiss needs to be called on UI thread (with Dispatcher).

Task.Delay(5000).ContinueWith(ctx => 
    this.Manager.Dismiss(builder.Message), 
    TaskScheduler.FromCurrentSynchronizationContext());

This should work if you don't want to make the calling method async and do the await call.

AleksandarDev commented 6 years ago

I'm planning to implement delayed dismiss with builder like it is specified here #5 . Is this what you had in mind?

@ghost1372 @arcticwhite

filipjelic commented 6 years ago

@AleksandarDev Yeah exactly that! It would be great with some kind of animation, like fade-out to disappear. But for now it's ok.

ghost1372 commented 6 years ago

i agree with @arcticwhite

AleksandarDev commented 6 years ago

@arcticwhite I'll create animation tak, but it's not on my priority list right now. Everyone is welcome to make PR for that...