Broken binding using the Prism, Silverlight, and ViewFirst approach

The problem we are facing is that we cannot become attached to work in our Silverlight Prism when we first use the viewing model approach. The first review approach works fine. We switched to official documentation and various websites, but still have not resolved the issue. Below is the code for the first presentation model, and the first look approach. Are we missing something? Read about it on my blog http://silvercasts.blogspot.com

The first approach to model presentation:

Loader:

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

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

ViewModel:

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

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

ViewModel Interface:

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

View Interface:

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

View Xaml:

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

View code behind:

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

View first approach

Loader:

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

ViewModel:

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

View code behind:

public View(IViewModel viewModel)
   {
       InitializeComponent();
       this.DataContext = viewModel;
   }
+3
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.

, .

+2

, DataContext " ", " ". , Prism DataContext ( , , ), DataContext, , . ViewModel View.SetModel() - DataContext?

0

, SetModel , . :


       public ViewModel(IView view)
       {
           View = view;

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

           View.SetModel(this);
       }

.

0

Source: https://habr.com/ru/post/1718381/


All Articles