简体   繁体   English

WPF - 在多个视图上共享相同的数据

[英]WPF - Share same data over multiple views

I'm currently setting up the structure for a simple editor for a game I'm working on. 我正在为我正在制作的游戏设置简单编辑器的结构。 This editor will basically load/save a serialized file, which is then used by the game to do whatever it desires. 这个编辑器基本上会加载/保存一个序列化文件,然后游戏会用它来做任何你想做的事情。

I see this serialized file pretty much as a database, as it contains data that's constantly used by multiple views, possibly at the same time. 我看到这个序列化文件几乎就像一个数据库,因为它包含多个视图经常使用的数据,可能同时存在。 As a result of that thought I figured to have all this data (lists of objects, strings, integers, whatever) in a centralized location which all views can read, update, add to, etcetera. 由于这个想法,我想在一个集中的位置拥有所有这些数据(对象,字符串,整数等等),所有视图都可以读取,更新,添加等等。

My first thought would go to a singleton class which has a reference to the deserialized data, then databind that accordingly so all open views recieve a notification when something changes at any time. 我的第一个想法是转到一个单例类,它引用了反序列化的数据,然后数据绑定,因此所有打开的视图在任何时候发生变化时都会收到通知。

But I was wondering, is there a way WPF would like you to solve this rather than (directly) binding to a singleton, static data, or a proxy property (property in a datacontext which just returns the data from the singleton)? 但我想知道,有没有一种方法WPF希望你解决这个问题,而不是(直接)绑定到单例,静态数据或代理属性(datacontext中的属性只返回单例中的数据)? Or is this just the way to go? 或者这只是要走的路?

As mentioned in the comments, the easiest way to share "data" between ViewModel is to pass a reference into them once each one is instantiated. 正如评论中所提到的,在ViewModel之间共享“数据”的最简单方法是在每个实例化后将引用传递给它们。

var sharedModel = new SharedModel();
var viewModel1 = new ViewModel1(sharedModel);
var viewModel2 = new ViewModel2(sharedModel);

Ctor for one of the shared ViewModel's will have whatever Model's that are required as a parameter. 其中一个共享ViewModel的Ctor将具有作为参数所需的任何模型。

public ViewModel1(SharedModel model) { ... }

If you do not want to manually 'inject' each dependency into associated ViewModel's, you can use Unity, which is an Inversion of Control (IoC) container that will automatically inject applicable dependencies into your ViewModels via Dependency Injection (DI). 如果您不想将每个依赖项手动“注入”到关联的ViewModel中,则可以使用Unity,它是一个控制反转(IoC)容器,它将通过依赖注入(DI)自动将适用的依赖项注入ViewModel。 With DI, you typically would want an associated Interface for each Model so that you are not using concrete types. 使用DI,您通常需要为每个模型建立一个关联的接口,以便您不使用具体类型。 You can also register concrete types as well. 您也可以注册具体类型。

So, for example, the new shared Model will look like: 因此,例如,新的共享模型将如下所示:

public class SharedModel : ISharedModel { ... }

and the ViewModels that need it, will have a new constructor and also an interface 以及需要它的ViewModel,将有一个新的构造函数和一个接口

public class ViewModel1 : IViewModel1
{
    public ViewModel1(ISharedModel model) { ... }   
}

then, using Unity you can Register your ViewModels and Models into the same IoC. 然后,使用Unity,您可以将ViewModel和模型注册到同一个IoC中。

var container = new UnityContainer();

// this will cause the Model to only be created once and shared to other types that have been registered in the container
//
container.RegisterType<ISharedModel, SharedModel>(new ContainerControlledLifetimeManager()); 
container.RegisterType<IViewModel1, ViewModel1>();
container.RegisterType<IViewModel2, ViewModel2>();

So now, everything lives in the Unity container. 所以现在,一切都存在于Unity容器中。 Once you Resolve needed ViewModels there is no need to automatically inject any dependencies, Unity will take care of it for you. 一旦解决了所需的ViewModel,就不需要自动注入任何依赖项,Unity会为您处理它。

var viewModel1 = container.Resolve<IViewModel1>();

If you want to learn more about DI and Unity, take a look at: http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx 如果您想了解有关DI和Unity的更多信息,请查看: http//msdn.microsoft.com/en-us/library/dn178463(v = pandp.30).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM