การเชื่อมโยงที่ไม่สมบูรณ์ด้วยวิธี Prism, Silverlight และ ViewFirst

ปัญหาที่เรากำลังประสบคือ เราไม่สามารถเชื่อมโยงการทำงานในแอปพลิเคชัน Prism Silverlight ของเราได้ เมื่อใช้วิธีการแรกของ view-model มุมมองแรกทำงานได้ดี เราได้ตรวจสอบเอกสารอย่างเป็นทางการและเว็บไซต์ต่างๆ แล้ว แต่ยังคงไม่สามารถแก้ไขปัญหาได้ ด้านล่างนี้เป็นโค้ดสำหรับทั้ง view-model ก่อน และ view first เราขาดอะไรบางอย่างไปหรือเปล่า? อ่านเกี่ยวกับเรื่องนี้ในบล็อกของฉัน http://silvercasts.blogspot.com

แนวทางแรกของ View-Model:

บูทสแตรปเปอร์:

   internal void RegisterLoginRegionAndView()
   {
       IRegionManager regionManager = Container.Resolve<IRegionManager>();

       regionManager.RegisterViewWithRegion(ShellRegionNames.MainRegion,
       () => Container.Resolve<IViewModel>().View);
    }

ดูรุ่น:

   public ViewModel(IView view)
   {
       View = view;
       View.SetModel(this);

       User = new User();
       User.Username = "TestUser";
   }

อินเทอร์เฟซ ViewModel:

 public interface IViewModel
   {
       IView View { get; set; }
   }

ดูอินเทอร์เฟซ:

public interface IView
   {
       void SetModel(IViewModel model);
   }

ดู Xaml:

 <TextBox x:Name="Username" TextWrapping="Wrap" Text="{Binding User.Username}" />

ดูโค้ดเบื้องหลัง:

public void SetModel(IViewModel viewModel)
   {
       this.DataContext = viewModel;
   }

ดูแนวทางแรก

บูทสแตรปเปอร์:

regionManager.RegisterViewWithRegion(ShellRegionNames.MainRegion, typeof(IView));

ดูรุ่น:

  public ViewModel()
   {
       User = new User();
       User.Username = "TestUser";
   }

ดูโค้ดเบื้องหลัง:

public View(IViewModel viewModel)
   {
       InitializeComponent();
       this.DataContext = viewModel;
   }

person cmaduro    schedule 23.09.2009    source แหล่งที่มา


คำตอบ (3)


การใช้งาน SetModel ในมุมมองของคุณจะต้องเป็นดังนี้:

public void MyUserControl : UserControl, IView
{
     //...
     public void SetModel(IViewModel vm)
     {
          this.DataContext = vm;
     }
}

หากไม่มี ก็ต้องเป็น (คุณยังไม่ได้โพสต์การใช้งาน SetModel แต่นี่จะเป็นสาเหตุของปัญหาในกรณีนี้)

หากไม่เป็นปัญหา อาจเป็นไปได้ว่า ViewModel ของคุณไม่ได้ใช้ INotifyPropertyChanged ฉันมักจะใช้ ViewModel พื้นฐานที่ทำสิ่งนี้:

public class ViewModelBase : INotifyPropertyChanged
{
     public event PropertyChangedEventHandler PropertyChanged;
     public void OnPropertyChanged(string propertyName)
     {
          if(PropertyChanged != null)
          {
               PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
     }
}

จากนั้น ViewModels ทั้งหมดของฉันก็มาจากสิ่งนั้น:

public class MyViewModel : ViewModelBase
{
     private User _user;
     public User User
     {
         get { return _user; }
         set
         {
              _user = value;
              OnPropertyChanged("User");
         }
     }
}

หมายเหตุ: ในกรณีของคุณ ออบเจ็กต์ "ผู้ใช้" ควรเป็น ViewModel และยังเพิ่ม OnPropertyChanged สำหรับคุณสมบัติ Username ด้วย

หวังว่านี่จะช่วยได้

person Anderson Imes    schedule 23.09.2009
comment
ฉันทำแล้วแต่การจัดรูปแบบเกิดความยุ่งเหยิง อยู่ใต้โค้ด XAML แต่จากที่ดูแล้วก็ดูดีนะ ขอบคุณ! - person cmaduro; 24.09.2009

ความแตกต่างที่ชัดเจนสำหรับฉันคือคุณตั้งค่า DataContext ในแนวทาง "ดูก่อน" แต่ไม่ใช่ในแนวทาง "ดูโมเดลก่อน" ฉันไม่แน่ใจว่า Prism ตั้งค่า DataContext ให้คุณหรือไม่ (ฉันเดาว่าคุณกำลังสันนิษฐานว่าเป็นเช่นนั้น) แต่ลองตั้งค่า DataContext ด้วยตนเองเพื่อดูว่านี่เป็นปัญหาหรือไม่ ในตัวสร้าง ViewModel ของคุณคุณเรียก View.SetModel(this) - การเรียกนั้นตั้งค่า DataContext หรือไม่

person James Cadd    schedule 23.09.2009

ปัญหาคือฉันใช้เมธอด SetModel ก่อนที่ออบเจ็กต์ข้อมูลจะถูกอินสแตนซ์ ย้ายดังนี้:



       public ViewModel(IView view)
       {
           View = view;

           User = new User();
           User.Username = "TestUser";

           View.SetModel(this);
       }

แก้ไขปัญหาแล้ว

person cmaduro    schedule 24.09.2009
comment
สิ่งนี้ทำให้เกิดปัญหาเนื่องจาก ViewModel ของคุณไม่ได้ใช้ INotifyPropertyChanged อย่างถูกต้อง นี่ไม่ควรเป็นปัญหา ฉันจะอัปเดตคำตอบของฉันโดยใช้คำฟุ่มเฟือยเพิ่มเติมเกี่ยวกับเรื่องนี้ - person Anderson Imes; 24.09.2009