我在下面的代码中实现了一个 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 更改,这将引发我的应用程序的窗口。但我不希望它被提出来。我该怎么办?我怎样才能防止我的吐司在激活时消失(换句话说,在点击吐司的任何部分时(?
我的第二个问题是,我想将 MediaPlayer 对象的位置绑定到进度条的值,但我的通知 toast 没有更新其值。如何保持值更新?如何保持状态更新(它是一个需要转换为的字符串(?
我的最后一个问题是,即使我将过期时间设置为Music.Duration,通常是几分钟,为什么我的吐司在几秒钟后消失了?
对不起,这么多问题。提前感谢!
Q1:已激活
您应该选择从后台执行任务,以便当您单击按钮时,该应用程序不会被激活。具体内容可以参考本文档。此外,当您单击Toast时,无法防止吐司在激活时消失。
Q2:更新
你可以使用吐司。要绑定进度的数据。具体步骤请参考本文档。
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
。吐司仍将显示在屏幕中,直到单击为止。