c# - UWP 通知 Toast 已激活、更新和过期

标签 c# xaml uwp notifications toast

我在下面的代码中实现了 toast :

    public void ShowToast(Music music)
    {
        var toastContent = new ToastContent()
        {
            Visual = new ToastVisual()
            {
                BindingGeneric = new ToastBindingGeneric()
                {
                    Children =
                    {
                        new AdaptiveText()
                        { 
                            Text = string.IsNullOrEmpty(music.Artist) ?
                                   string.IsNullOrEmpty(music.Album) ? music.Name : string.Format("{0} - {1}", music.Name, music.Album) :
                                   string.Format("{0} - {1}", music.Name, string.IsNullOrEmpty(music.Artist) ? music.Album : music.Artist)
                        },
                        new AdaptiveProgressBar()
                        {
                            Value = new BindableProgressBarValue("MediaControl.Position"),
                            ValueStringOverride = MusicDurationConverter.ToTime(music.Duration),
                            Title = "Lyrics To Be Implemented",
                            Status = MusicDurationConverter.ToTime(MediaControl.Position)
                        }
                    }
                }
            },
            Actions = new ToastActionsCustom()
            {
                Buttons =
                {
                    new ToastButton("Pause", "Pause")
                    {
                        ActivationType = ToastActivationType.Background
                    },
                    new ToastButton("Next", "Next")
                    {
                        ActivationType = ToastActivationType.Background
                    }
                },
            },
            Launch = "Launch",
            Audio = Helper.SlientToast,
        };

        // Create the toast notification
        var toast = new ToastNotification(toastContent.GetXml())
        {
            ExpirationTime = DateTime.Now.AddSeconds(music.Duration),
        };
        toast.Activated += Toast_Activated;
        Helper.ShowToast(toast);
    }

    private async void Toast_Activated(ToastNotification sender, object args)
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
        {
            switch ((args as ToastActivatedEventArgs).Arguments)
            {
                case "Next":
                    MediaControl.NextMusic();
                    break;
                case "Pause":
                    PauseMusic();
                    break;
                default:
                    break;
            }
        });
    }

我想在我的应用窗口不可见时发送音乐通知。

我的第一个问题是,暂停和下一步操作会触发一些 UI 更改,这会提升我的应用程序的窗口。但我不希望它被提升。我应该怎么办?我怎样才能防止我的 toast 在激活时消失(换句话说,在单击 toast 的任何部分时)?

我的第二个问题是,我想将 MediaPlayer 对象的位置绑定(bind)到进度条的值,但我的通知 toast 没有更新它的值。我怎样才能保持值(value)更新?如何保持状态更新(它是一个需要转换成的字符串)?

我的最后一个问题是,即使我将过期时间设置为 Music.Duration,通常是几分钟,但为什么我的 toast 会在几秒钟后消失?

抱歉问了这么多问题。提前致谢!

最佳答案

Q1:Activated

您应该选择从后台执行任务,这样当您点击按钮时,应用程序不会被激活。具体内容可以引用这个document .另外,点击激活后toast消失是不可能的。

Q2:Update

可以使用toast.Data绑定(bind)进度,具体步骤可以引用这个document .

new AdaptiveProgressBar()
                        {​
                            Value = new BindableProgressBarValue("progressValue"),​
                            ValueStringOverride = new BindableString("progressValueString"),​
                            Status = new BindableString("progressStatus")​
                        }

string tag = "Myplaylist";
string group = "playing";​
toast.Tag = tag;​
toast.Group = group;​
toast.Data = new NotificationData();​
toast.Data.Values["progressValue"] = "0.0";​
toast.Data.Values["progressValueString"] = "My first song";​
toast.Data.Values["progressStatus"] = "Playing...";​
toast.Data.SequenceNumber = 0;

当你想更新进度时,调用下面的方法。

public void UpdateProgress()
        {​
            // Construct a NotificationData object;​
            string tag = "Myplaylist";​
            string group = "playing";​
​
            // Create NotificationData and make sure the sequence number is incremented​
            // since last update, or assign 0 for updating regardless of order​
            var data = new NotificationData​
            {​
                SequenceNumber = 0​
            };​

            data.Values["progressValue"] = "0.7";​
            data.Values["progressValueString"] = "My first song";​
​
            // Update the existing notification's data by using tag/group​
            ToastNotificationManager.CreateToastNotifier().Update(data, tag, group);​
        }

Q3:Expire

过期时间的设置是指消息中心列表清除消息的时间,而不是toast保留的时间。解决方法是:可以在toastContent中设置Scenario = ToastScenario.Reminder。toast仍然会出现在屏幕上直到被点击。

关于c# - UWP 通知 Toast 已激活、更新和过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57543321/

相关文章:

c# - 将自定义对象分配给 SSIS 中的对象类型变量

c# - 如何让套接字在 Unity 和 UWP 应用程序之间工作?

wpf - 将 MessageBoxImage 转换为 ImageSource WPF

c# - UWP toast 没有振动?

c# - 模型方法绑定(bind)在 xaml 中不起作用

c# - NLog 自动截断消息

c# - "Request entity too large"和锁定的配置部分

c# - 在 WPF xaml 文件中引用另一个项目中的 namespace

WPF 列表框数据绑定(bind)适用于设计器,但不适用于运行时

c# - DependencyProperty 的 PropertyChangedCallback 忽略延迟绑定(bind)