如何在 WPF MVVM 应用程序中处理可绑定的应用程序范围变量?
我正在使用 MVVM 模式编写一个相当大规模的 WPF 桌面应用程序。我已经被困了一段时间,无法在视图中更新我的公共属性,而不是在更新它的视图中。
我有一个 RibbonWindow MainView
,其中包含一个 ContentControl
,它根据用户的选择一次显示一个剩余的视图。我有一个所有 ViewModel 都扩展的 BaseViewModel 类。除其他外,此类公开 INotifyPropertyChanged 接口并包含 CommonDataStore
类型的 static
属性。此类还实现 INotifyPropertyChanged 接口并包含每个 ViewModel 可用的属性。
现在,虽然我可以从任何 ViewModel 访问并成功更新 CommonDataStore
属性,但问题是 WPF 框架只会通知当前视图中已更改的属性。因此,尽管其他 ViewModel 中的通用值已更新,但其关联的 View 并未更新。
我的应用程序的一个示例是登录屏幕:当用户登录时,我的 LogInView 会使用数据库中的新信息(即全名)进行更新,但 MainView 中的用户详细信息不会更新。
在阅读了其他几篇文章后,我还尝试将 CommonDataStore 类实现为单例,但这并没有帮助。我还可以将从 MainViewModel
中将此公共数据对象的引用传递给每个 ViewModel 的构造函数,但我不确定这是否是正确的方法。
我还发现,在 WPF 中,static
属性的处理方式有点像常量值。看起来他们只读取了一次值。
所以无论如何,很明显,我的尝试都失败了。我想知道这样做的标准方法是什么?特别是,我需要能够绑定到公共属性,并在任何公共值发生更改时更新所有 ViewModel 和 View。任何帮助将不胜感激。非常感谢。
编辑>>真的吗?没有人在 MVVM WPF 应用程序中使用应用程序范围的变量?
我现在已经删除了 Common 属性声明的静态部分,只是将一个副本单独传递到每个 ViewModel 中。这似乎有效,但我真的很想知道其他人如何处理这种情况。请简单地告诉我您如何组织此应用程序范围的数据来回答。
I am writing a fairly large scale WPF desktop application using the MVVM pattern. I have been stuck for a while on getting my common properties to update in a View other than the one that updated it.
I have a RibbonWindow MainView
that contains a ContentControl
that displays the remaining Views one at a time dependant on the user's selection. I have a BaseViewModel
class that all the ViewModels extend. Among other things, this class exposes the INotifyPropertyChanged interface and contains a static
property of type CommonDataStore
. This class also implements the INotifyPropertyChanged interface and contains the properties that are to be available to every ViewModel.
Now, although I can access and successfully update the CommonDataStore
properties from any ViewModel, the problem is that the WPF Framework will only notify properties that have changed in the current View. Therefore, although the common values have been updated in other ViewModels, their associated Views do not get updated.
One example from my application is the login screen: As the user logs in, my LogInView updates with the new information (ie. full name) from the database, but the user details in the MainView do not.
After reading a few other posts, I also tried implementing the CommonDataStore
class as a Singleton, but that didn't help. I could also just pass a reference to this common data object to the constructor of each ViewModel from the MainViewModel
, but I'm not sure if this is the right way to go.
I have also discovered that in WPF, static
properties are treated a bit like constant values. It seems that they just read the value once.
So anyway it's clear, my attempts have all failed. I was wondering what the standard way of doing this was? In particular, I need to be able to bind to the common properties and have all of my ViewModels and Views update when any common value is changed. Any help would be greatly appreciated. Many thanks in advance.
Edit >> Really? No one uses application wide variables in an MVVM WPF application?
I have now removed the static part of the Common property declaration and am simply passing a copy into each ViewModel individually. This seems to work, but I'd really like to know how others approach this situation. Please answer by simply letting me know how you organise this application wide data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我做了一些类似于你最后描述的事情。我有一个名为 SecurityContext 的类,它保存一些应用程序范围的数据。应用程序启动时会创建一个实例,然后该实例通过依赖注入传递到所有 ViewModel 的构造函数中。我有一个 ViewModel 的基类,它通过常规实例属性公开该对象(实现 INotifyPropertyChanged)。
I have done something similar to what you describe last. I have class called SecurityContext that holds some of the application-wide data. One instance is created when the application starts up and then that instance is passed into the constructors of all the ViewModels through dependency-injection. I have a base class for ViewModels which exposes that object through a regular instance property (implementing INotifyPropertyChanged).
您是否考虑过实施观察者模式?我们已经通过 IObservable 和 IObserver。 这描述了“IObservable/IObserver开发模型”如下:
IObservable/IObserver 开发模型提供了使用输入和输出适配器作为事件源和接收器的生产者和消费者的替代方案。该模型基于 IObservable/IObserver 设计模式,其中观察者是希望在另一个对象的状态发生变化时收到通知的任何对象,而可观察者是其状态可能感兴趣的任何对象,并且另一个对象可能对其状态感兴趣。登记兴趣。例如,在发布-订阅应用程序中,可观察者是发布者,观察者是订阅者对象。有关详细信息,请参阅 MSDN 上的探索观察者设计模式。
Have you looked into implementing the Observer Pattern? We have done so with IObservable and IObserver. This describes the "IObservable/IObserver Development Model" as follows:
The IObservable/IObserver development model provides an alternative to using input and output adapters as the producer and consumer of event sources and sinks. This model is based on the IObservable/IObserver design pattern in which an observer is any object that wishes to be notified when the state of another object changes, and an observable is any object whose state may be of interest, and in whom another object may register an interest. For example, in a publication-subscription application, the observable is the publisher, and the observer is the subscriber object. For more information, see Exploring the Observer Design Pattern on MSDN.