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;
}
});
}
我想在我的应用 window 不可见时发送音乐通知。
我的第一个问题是,暂停和下一步操作会触发一些 UI 更改,这会提高我的应用程序的 window。但我不希望它被提升。我应该怎么办?我怎样才能防止我的 toast 在激活时消失(换句话说,在单击 toast 的任何部分时)?
我的第二个问题是,我想将 MediaPlayer 对象的位置绑定到进度条的值,但我的通知 toast 没有更新它的值。我怎样才能保持价值更新?如何保持状态更新(是需要转换成的字符串)?
我的最后一个问题是,即使我将过期时间设置为 Music.Duration,通常是几分钟,为什么我的 toast 会在几秒钟后消失?
抱歉问了这么多问题。提前致谢!
Q1:Activated
你应该选择从后台执行任务,这样当你点击按钮时,应用程序不会activated.The具体内容你可以参考这个document.另外,无法防止toast在点击激活后消失
Q2:Update
可以使用toast.Data绑定progress.The具体步骤可以参考这个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的时间kept.The解决方法是:可以设置Scenario = ToastScenario.Reminder in toastContent
。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;
}
});
}
我想在我的应用 window 不可见时发送音乐通知。
我的第一个问题是,暂停和下一步操作会触发一些 UI 更改,这会提高我的应用程序的 window。但我不希望它被提升。我应该怎么办?我怎样才能防止我的 toast 在激活时消失(换句话说,在单击 toast 的任何部分时)?
我的第二个问题是,我想将 MediaPlayer 对象的位置绑定到进度条的值,但我的通知 toast 没有更新它的值。我怎样才能保持价值更新?如何保持状态更新(是需要转换成的字符串)?
我的最后一个问题是,即使我将过期时间设置为 Music.Duration,通常是几分钟,为什么我的 toast 会在几秒钟后消失?
抱歉问了这么多问题。提前致谢!
Q1:Activated
你应该选择从后台执行任务,这样当你点击按钮时,应用程序不会activated.The具体内容你可以参考这个document.另外,无法防止toast在点击激活后消失
Q2:Update
可以使用toast.Data绑定progress.The具体步骤可以参考这个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的时间kept.The解决方法是:可以设置Scenario = ToastScenario.Reminder in toastContent
。toast仍然会出现在屏幕直到被点击。