การส่งข้อความทั่วไป

public class Foo<T> where T: Entity
{}

public class Foo1
: Foo<Telephone>
{
}

public class Foo2
: Foo<Home>
{   
}

ฉันจะส่ง Foo1 ไปยัง Foo2 ได้อย่างไร ฉันรู้ว่ามีการพิมพ์ข้อความ และด้วยเหตุนี้ข้อความที่ได้รับประเภทเดียวกัน - แต่ฉันต้องส่งข้อความระหว่างคลาสที่ได้รับ...

ตัวอย่างจะได้รับการชื่นชมอย่างมาก


person codeputer    schedule 26.05.2011    source แหล่งที่มา


คำตอบ (2)


ตามทฤษฎีแล้วการส่งข้อความใน 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
comment

คุณพลาดการมอบหมาย EventHandler ที่ไหนสักแห่งหรือไม่? ฉันไม่เห็นที่นี่

ฉันพนันได้เลยว่าคุณไม่มีโค้ดของนักออกแบบที่มีลักษณะดังต่อไปนี้:

this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);

นี่เป็นปัญหาของการใช้โค้ดที่สร้างขึ้นอัตโนมัติจากการลากและวาง GUI เมื่อถูกงูกัด ผู้คนไม่รู้ว่าจะแก้ไขอย่างไร

ลองลบ BackgroundWorker แล้วเริ่มต้นใหม่

- person codeputer; 27.05.2011

อีกทางเลือกหนึ่งคือการสร้างคลาสของคุณเองที่มีเพย์โหลดที่คุณต้องการส่งมอบ (Foo1 หรือเพียงแค่ object) จากนั้นใน Foo2 ให้ลงทะเบียนเพื่อรับข้อความประเภทชั้นเรียนที่คุณเพิ่งสร้างขึ้น

ลิงค์นี้อธิบายวิธีการด้วยตัวอย่างที่เข้าใจง่ายมาก

MVVM Light Toolkit Soup To Nuts 3 - Jesse Liberty

person Vincenzo Piscitelli    schedule 27.05.2011