与 Prism、Silverlight 和 ViewFirst 方法的绑定中断

发布于 2024-08-05 19:09:33 字数 1735 浏览 2 评论 0原文

我们遇到的问题是我们无法在我们的环境中绑定工作 首先使用视图模型时的 prism silverlight 应用程序 方法。视图优先方法工作得很好。我们已经回顾了 官方文档和各种网站,但还没有 解决了这个问题。下面首先是视图模型的代码, 和视图优先方法。我们错过了什么吗?在我的博客上阅读相关内容 http://silvercasts.blogspot.com

视图模型第一种方法:

Bootstrapper:

   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 接口:

 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;
   }

查看第一种方法

Bootstrapper:

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

ViewModel:

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

查看代码隐藏:

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

The problem we are having is that we cannot get binding to work in our
prism silverlight application when using the view-model first
approach. The view first approach work fine. We have gone over the
official documentation and various web sites, but have still not
resolved the issue. Below is the code for both the view-model first,
and the view first approach. Are we missing something? Read about it on my blog http://silvercasts.blogspot.com

View-Model first approach:

Bootstrapper:

   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

Bootstrapper:

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;
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬﹪忆之殇 2024-08-12 19:09:33

您的视图上的 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));
          }
     }
}

然后我的所有 ViewModel 都从中派生:

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

注意: 在您的情况下,“User”对象可能也应该是 ViewModel 并且还为用户名引发 OnPropertyChanged财产。

希望这有帮助。

Your implementation of SetModel on your view needs to be as follows:

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

If that's not there, it needs to be (you haven't posted your implementation of SetModel, but this would be the source of the issue in this case).

If this is not the issue, it's likely because your ViewModel does not implement INotifyPropertyChanged. I usually use a base ViewModel that does this:

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

And then all of my ViewModels derive from that:

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

Note: in your case the "User" object should probably also be a ViewModel and also raise OnPropertyChanged for the Username property.

Hope this helps.

后来的我们 2024-08-12 19:09:33

对我来说,明显的区别是您在“视图优先”方法中设置 DataContext,而不是在“视图模型优先”方法中设置。我不确定 Prism 是否为您设置了 DataContext(我猜您假设它确实如此),但请尝试手动设置 DataContext 以查看这是否是问题所在。在 ViewModel 构造函数中,您调用 View.SetModel(this) - 该调用是否设置 DataContext?

The obvious difference to me is that you set the DataContext in the "view first" approach, but not in the "view model first" approach. I'm not sure if Prism sets the DataContext for you (I'd guess that you're assuming that it does) but try setting the DataContext manually to see if this is the problem. In your ViewModel constructor you call View.SetModel(this) - does that call set the DataContext?

神也荒唐 2024-08-12 19:09:33

问题是我在实例化数据对象之前使用了 SetModel 方法。像这样移动:

       public ViewModel(IView view)
       {
           View = view;

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

           View.SetModel(this);
       }

解决了问题。

The problem was that I was using the SetModel method before the data object was instanced. Moving it like this:


       public ViewModel(IView view)
       {
           View = view;

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

           View.SetModel(this);
       }

solved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文