与 Prism、Silverlight 和 ViewFirst 方法的绑定中断
我们遇到的问题是我们无法在我们的环境中绑定工作 首先使用视图模型时的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的视图上的
SetModel
实现需要如下所示:如果不存在,则需要如此(您还没有发布
SetModel
的实现,但这会是本例中问题的根源)。如果这不是问题,则可能是因为您的 ViewModel 未实现
INotifyPropertyChanged
。我通常使用执行此操作的基本 ViewModel:然后我的所有 ViewModel 都从中派生:
注意: 在您的情况下,“User”对象可能也应该是 ViewModel 并且还为用户名引发 OnPropertyChanged财产。
希望这有帮助。
Your implementation of
SetModel
on your view needs to be as follows: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:And then all of my ViewModels derive from that:
Note: in your case the "User" object should probably also be a ViewModel and also raise OnPropertyChanged for the Username property.
Hope this helps.
对我来说,明显的区别是您在“视图优先”方法中设置 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?
问题是我在实例化数据对象之前使用了 SetModel 方法。像这样移动:
解决了问题。
The problem was that I was using the SetModel method before the data object was instanced. Moving it like this:
solved the problem.