我已经看过几个如何将验证消息发送回UI,同时编辑内容的示例。
public class StandardPageValidator : IValidate<standardpage>
{
IEnumerable<validationerror> IValidate<standardpage>.Validate(StandardPage instance)
{
// Silly example to validate if the PageName and MainBody properties start with the same letter
if (instance.PageName.StartsWith(EPiServer.Core.Html.TextIndexer.StripHtml(instance.MainBody.ToHtmlString().Substring(0, 1), int.MaxValue)))
{
return new[] { new ValidationError() {
ErrorMessage = "Main body and PageName cannot start with the same letter",
PropertyName = "PageName", RelatedProperties = new string[] { "MainBody" },
Severity = ValidationErrorSeverity.Error,
ValidationType = ValidationErrorType.AttributeMatched
} };
}
return new ValidationError[0];
}
}
但是我想在拦截已发布的内容事件后向UI发送消息,但是这个方法返回void,那么我该怎么办呢?
public void Initialize(InitializationEngine context)
{
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.PublishedContent += EventsPublishedContent;
}
private void EventsPublishedContent(object sender, ContentEventArgs e)
{
if (e.Content is myType)
{
//do some business logic work....
//How can I send a Info Message back to the UI here?
}
}
答案 0 :(得分:1)
您可以在代码示例中的EventsPublishedContent
内执行此操作:
e.CancelAction = true;
e.CancelReason = "This message will be displayed in the UI.";