簡體   English   中英

UWP 通知 Toast 已激活、更新和過期

[英]UWP Notification Toast Activated, Update and Expire

我在下面的代碼中實現了一個祝酒詞:

    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;
            }
        });
    }

我想在我的應用程序窗口不可見時發送音樂通知。

我的第一個問題是,Pause 和 Next 操作將觸發一些 UI 更改,這將提升我的應用程序的窗口。 但我不希望它被提出來。 我該怎么辦? 以及如何防止我的吐司在激活時消失(換句話說,點擊吐司的任何部分)?

我的第二個問題是,我想將 MediaPlayer 對象的位置綁定到進度條的值,但我的通知 toast 沒有更新其值。 如何保持值更新? 如何保持狀態更新(它是一個需要轉換為的字符串)?

我的最后一個問題是,即使我將過期時間設置為 Music.Duration,通常是幾分鍾,為什么我的 toast 會在幾秒鍾后消失?

抱歉問了這么多問題。 提前致謝!

Q1:激活

您應該選擇從后台執行任務,這樣當您點擊按鈕時,應用程序不會被激活。具體內容您可以參考此文檔。另外,無法防止激活時toast消失點擊它。

Q2:更新

可以使用toast.Data來綁定進度。具體步驟可以參考這個文檔

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:過期

過期時間設置是指消息中心列表清除消息的時間,而不是toast的Scenario = ToastScenario.Reminder in toastContent 。解決方法是:可以Scenario = ToastScenario.Reminder in toastContent設置Scenario = ToastScenario.Reminder in toastContent toast在點擊之前仍然會出現在屏幕上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM