Hy guys!
I am currently working on a little WPF project using MVVM via the Onyx framework.
我目前正在通过Onyx框架使用MVVM开发一个小WPF项目。
My currentview architecture is like this:
我的currentview架构是这样的:
<DockPanel>
<Menu DockPanel.Dock="Top" Background="#cecece">
<!-- Menu -->
</Menu>
<Grid>
<views:TranslationView x:Name="translationView" />
</Grid>
</DockPanel>
The question that I now have is how to implement the relationship between the two viewmodels. I currently basically just have the TranslationView have its own ViewModel with no link to the parent ViewModel.
我现在的问题是如何实现两个视图模型之间的关系。我目前基本上只是让TranslationView拥有自己的ViewModel,没有链接到父ViewModel。
The problem is that I want to be able to open some file via the MainView and then parse the contents and display them in the TranslationView. Is there a recommended way to do this?
问题是我希望能够通过MainView打开一些文件,然后解析内容并在TranslationView中显示它们。有推荐的方法吗?
I thought about just using the TranslationViewModel as a property in the MainViewModel and then using it as DataContext for the TranslationView but it kinda seems to run against Onyx's model to define the ViewModel through a type (not a object) reference.
我想过只使用TranslationViewModel作为MainViewModel中的属性,然后将其用作TranslationView的DataContext,但有点似乎是针对Onyx的模型运行,以通过类型(非对象)引用定义ViewModel。
1 个解决方案
#1
Onyx doesn't have a "model to define the ViewModel through a type (not a object) reference". The ViewModel attached property can be assigned an object reference. In fact, this property is an Object type and uses coercion to change a Type instance into an object instance of the specified Type. This is a convenience only. You're free to just assign an object reference created in any way you want.
Onyx没有“通过类型(非对象)引用定义ViewModel的模型”。可以为ViewModel附加属性分配对象引用。实际上,此属性是Object类型,并使用强制将Type实例更改为指定Type的对象实例。这只是一种便利。您可以*地分配以您想要的任何方式创建的对象引用。
Like most questions, there's any number of ways to solve your problem. The solution you mention but dismissed because you thought it went against Onyx's design is one that could work, but I'd hesitate to use, simply because it creates tighter coupling. Another solution would be to utilize the Event Agregator pattern to communicate between the views in a disconnected fashion. Or you could utilize a more service oriented approach. For example, I usually define an IApplication service that stands in where you'd normally access Application.Current in a tightly coupled design. You could provide a property on this service to hold the contents of the loaded file, and expose INotifyPropertyChanged on the service to allow the TranslationViewModel to know that the property was changed.
像大多数问题一样,有很多方法可以解决您的问题。您提到但解雇的解决方案是因为您认为它违背了Onyx的设计是可行的,但我会犹豫使用,因为它会产生更紧密的耦合。另一种解决方案是利用Event Agregator模式以断开连接的方式在视图之间进行通信。或者您可以使用更加面向服务的方法。例如,我通常定义一个IApplication服务,它位于您通常以紧密耦合设计访问Application.Current的位置。您可以在此服务上提供属性以保存已加载文件的内容,并在服务上公开INotifyPropertyChanged以允许TranslationViewModel知道该属性已更改。
#1
Onyx doesn't have a "model to define the ViewModel through a type (not a object) reference". The ViewModel attached property can be assigned an object reference. In fact, this property is an Object type and uses coercion to change a Type instance into an object instance of the specified Type. This is a convenience only. You're free to just assign an object reference created in any way you want.
Onyx没有“通过类型(非对象)引用定义ViewModel的模型”。可以为ViewModel附加属性分配对象引用。实际上,此属性是Object类型,并使用强制将Type实例更改为指定Type的对象实例。这只是一种便利。您可以*地分配以您想要的任何方式创建的对象引用。
Like most questions, there's any number of ways to solve your problem. The solution you mention but dismissed because you thought it went against Onyx's design is one that could work, but I'd hesitate to use, simply because it creates tighter coupling. Another solution would be to utilize the Event Agregator pattern to communicate between the views in a disconnected fashion. Or you could utilize a more service oriented approach. For example, I usually define an IApplication service that stands in where you'd normally access Application.Current in a tightly coupled design. You could provide a property on this service to hold the contents of the loaded file, and expose INotifyPropertyChanged on the service to allow the TranslationViewModel to know that the property was changed.
像大多数问题一样,有很多方法可以解决您的问题。您提到但解雇的解决方案是因为您认为它违背了Onyx的设计是可行的,但我会犹豫使用,因为它会产生更紧密的耦合。另一种解决方案是利用Event Agregator模式以断开连接的方式在视图之间进行通信。或者您可以使用更加面向服务的方法。例如,我通常定义一个IApplication服务,它位于您通常以紧密耦合设计访问Application.Current的位置。您可以在此服务上提供属性以保存已加载文件的内容,并在服务上公开INotifyPropertyChanged以允许TranslationViewModel知道该属性已更改。