在 Silverlight 中使用 PRISM 模式重新绑定控件
我一直在尝试使用复合应用程序库(Prism),并且我已经按照 Microsoft 的教程建立了一个非常标准的模式。基本上,视图被注入到区域中。视图是动态构建的,添加控件等都是以编程方式进行的。
我有一个被触发的命令,在回发时我想重新绑定当前视图上的控件,而不是完全重新渲染所有控件。
因此,我尝试使用更新版本更新模型,希望这会强制重新绑定控件。那是行不通的。不知道我应该采取什么方法,因为我是棱镜新手......
有什么想法吗?
订阅事件来处理回发
IEventAggregator aggregator = this.Container.Resolve<IEventAggregator>();
aggregator.GetEvent<DataInstanceLoadedEvent>().Subscribe(this.OnDataInstanceUpdated);
事件的实现
public void OnDataInstanceUpdated(DataInstance updatedInstance)
{
if(this.View.Model != null){
// We need to rebind here
IRegion region = this.LocateRegion(this.View); // gets the region....
this.View.Model.CurrentDataInstance = updatedInstance; // update the model instance
}
else{
// Render all controls over again since view.model is null ...
}
}
I have been trying to work with the Composite Application Library (Prism) and I have set up a pretty standard pattern that I have followed off Microsoft's tutorial. Basically, the View is injected into the Region. The View is dynamically built, adding controls and so forth all programmatically.
I have a command that gets fired and on postback I would like to rebind the controls on the current view, instead of completely re-rendering all the controls over again.
So I tried updating the model with the updated version hoping that would force a rebinding of controls. That doesn't work. Not sure what approach I should be taking, for I am new to Prism...
Any ideas?
Subscribe an event to handle postbacks
IEventAggregator aggregator = this.Container.Resolve<IEventAggregator>();
aggregator.GetEvent<DataInstanceLoadedEvent>().Subscribe(this.OnDataInstanceUpdated);
Implementation of the event
public void OnDataInstanceUpdated(DataInstance updatedInstance)
{
if(this.View.Model != null){
// We need to rebind here
IRegion region = this.LocateRegion(this.View); // gets the region....
this.View.Model.CurrentDataInstance = updatedInstance; // update the model instance
}
else{
// Render all controls over again since view.model is null ...
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经弄清楚如何根据微软建议的模式重新绑定。
基本上,我所要做的就是从模型上的
INotifyPropertyChanged
继承。然后按照这种模式,一旦我的模型更新,它就会通过触发一个事件来强制重新绑定所有控件,该事件通知客户端属性实际上已更改。
I have figured out how to rebind according to the suggested patterns from Microsoft.
Basically, all I had to do was inherit from
INotifyPropertyChanged
on my Model.Then following this pattern, once my model updates it is then forced to rebind all the controls by firing an event that notifies the client that the property has in fact changed.