在 Silverlight 中使用 PRISM 模式重新绑定控件

发布于 2024-09-06 14:05:44 字数 897 浏览 5 评论 0原文

我一直在尝试使用复合应用程序库(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 技术交流群。

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

发布评论

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

评论(1

迷爱 2024-09-13 14:05:44

我已经弄清楚如何根据微软建议的模式重新绑定。

基本上,我所要做的就是从模型上的 INotifyPropertyChanged 继承。

然后按照这种模式,一旦我的模型更新,它就会通过触发一个事件来强制重新绑定所有控件,该事件通知客户端属性实际上已更改。

public class MyModel : INotifyPropertyChanged
{
    private DataInstance currentDataInstance;
    public event PropertyChangedEventHandler PropertyChanged;
    public DataInstance CurrentDataInstance
    {
        get
        {
            return this.currentDataInstance;
        }
        set
        {
            if ( this.currentDataInstance == value )
                return;
            this.currentDataInstance = value;
            this.OnPropertyChanged( new PropertyChangedEventArgs("CurrentDataInstance"));
        }
    }
    protected virtual void OnPropertyChanged( PropertyChangedEventArgs e )
    {
        if ( this.PropertyChanged != null )
            this.PropertyChanged( this, e );
    }
}

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.

public class MyModel : INotifyPropertyChanged
{
    private DataInstance currentDataInstance;
    public event PropertyChangedEventHandler PropertyChanged;
    public DataInstance CurrentDataInstance
    {
        get
        {
            return this.currentDataInstance;
        }
        set
        {
            if ( this.currentDataInstance == value )
                return;
            this.currentDataInstance = value;
            this.OnPropertyChanged( new PropertyChangedEventArgs("CurrentDataInstance"));
        }
    }
    protected virtual void OnPropertyChanged( PropertyChangedEventArgs e )
    {
        if ( this.PropertyChanged != null )
            this.PropertyChanged( this, e );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文