ตามทฤษฎีแล้วการส่งข้อความใน mvvmlight นั้นควรจะลุกเป็นไฟและลืมไป...ผู้ส่งไม่สนใจว่าใครจะได้รับข้อความและผู้รับก็ไม่สนใจว่าใครจะส่งข้อความ ตราบใดที่มันเป็นประเภทที่ถูกต้องที่จะรับฟัง ฉันพบว่าผ่านการลองผิดลองถูกหลายครั้งว่าการสร้างข้อความของคุณเองนั้นง่ายกว่าการใช้ค่าเริ่มต้นที่ได้รับจาก mvvm-light ซึ่งเป็นจุดเริ่มต้นที่ดี แต่บางครั้งคุณจะพบว่าตัวเองกำลังกระโดดข้ามห่วง..
public class ExceptionMessage : GalaSoft.MvvmLight.Messaging.GenericMessage<System.Exception>
{
public ExceptionMessage(System.Exception content) : base(content) { }
public ExceptionMessage(object sender, System.Exception content) : base(sender, content) { }
public ExceptionMessage(object sender, object target, System.Exception content) : base(sender, target, content) { }
}
รหัสตัวรับ:
Messenger.Default.Register<Core.Messaging.ExceptionMessage>(this, ex => ShowExceptionMessage(ex));
รหัสผู้ส่ง:
public void LogException(Exception content)
{
_messenger.Send<Core.Messaging.ExceptionMessage>(new ExceptionMessage(content));
//GetBw().RunWorkerAsync(content);
WriteToDatabaseLog(content);
}
และใช่ สิ่งนี้ขัดต่อข้อเสนอแนะในประโยคแรกของฉัน แต่ตามทฤษฎีแล้ว ฉันสามารถมี vms หลายรายการหรือดูข้อความยกเว้นได้
อาจเป็นอีกตัวอย่างที่จะช่วยคุณได้... ฉันเกลียดเรื่องทั้งหมด...มันทำให้ฉันสับสนเสมอ...
นี่คือโมดูลหลักของฉัน:
public class SaveNotification<T> : GalaSoft.MvvmLight.Messaging.NotificationMessage<T> where T : GalaSoft.MvvmLight.ViewModelBase
{
public SaveNotification(T content, string notification) : base(content, notification) { }
public SaveNotification(object sender, T content, string notification) : base(sender, content, notification) { }
public SaveNotification(object sender, object target, T content, string notification) : base(sender, target, content, notification) { }
}
นี่คือวิธีที่ฉันใช้มันใน vm ของฉัน:
public void OnSubmitChanges(SubmitOperation so)
{
if (so.HasError)
{
Infrastructure.GetService<IExceptionLoggingInterface>().LogException(this, so.Error);
}
else
{
//Save Responses
_messenger.Send<Messages.NavigationRequest<SubClasses.URI.PageURI>>(GetNavRequest_HOME());
ClearQuestionaire(true);
_messenger.Send<WavelengthIS.Core.Messaging.SaveNotification<QuestionairreViewModel>>(GetSuccessfulSaveNotification());
}
Wait.End();
}
private WavelengthIS.Core.Messaging.SaveNotification<QuestionairreViewModel> GetSuccessfulSaveNotification()
{
return new WavelengthIS.Core.Messaging.SaveNotification<QuestionairreViewModel>(this, "Save Successfull");
}
person
ecathell
schedule
27.05.2011