执行第一个代码隐藏或查看模型 [英] Which executes first Code Behind or View Model

查看:69
本文介绍了执行第一个代码隐藏或查看模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于我之前的问题 使用隐藏代码从 XAML 访问变量和从 ViewModel 访问对象:我怎么知道哪个先执行?

Based on my previous question Accessing variables from XAML and object from ViewModel using Code Behind: How would I know which executes first?

是背后的代码还是ViewModel?
我只想确保我的代码在 ViewModel 之前执行

Is it the code behind or the ViewModel?
I just want to make sure that my code behind executes prior the ViewModel

推荐答案

View 和 ViewModel 都是被实例化的常规类.这是通过像在任何其他类中一样调用构造函数来完成的.因此,作为对您问题的简单回答:在每个构造函数中设置一个断点,然后查看哪个首先被命中.

The View and the ViewModel are both regular classes that get instantiated. This is done by calling the constructor as in any other class. So, as a simple answer to your question: Set a breakpoint in each constructor and see which one gets hit first.

您的问题没有通用答案,因为这取决于您的架构和用例.通常,某些控件绑定到其父级的 ViewModel 属性,该属性会在某些时候发生变化.那时您的 View 已经存在,并且您不知道属性设置的值已经存在多长时间.在其他情况下,您的 View 是为特定的 ViewModel 创建的,并将其作为构造函数参数.

There is no general answer to your question because it depends on your architecture and use case. Often, some control is bound to a property of the ViewModel of it's parent, which changes at some point. At that point your View already exists and you have no idea how long the value to which the property has been set is existing already. In other cases, your View is created for a specific ViewModel and takes it as constructor parameter.

确保 ViewModel 在 View 之前存在的一种方法是将 ViewModel 作为构造函数参数传递.构造函数参数背后的想法是表达:这个类需要创建 xy 类型的现有实例",这就是您所要求的.但是,由于您将在构造函数中将其设置为视图 DataContext 并且 DataContext 可以在创建视图后更改,因此您无法确定视图不会创建后分配一个新的 ViewModel.更糟糕的是,您将无法再在 XAML 中使用您的控件,因为它不再具有默认构造函数.

The one way to make sure that the ViewModel exists before the View is to pass the ViewModel as a constructor parameter. The idea behind constructor parameters is to express: "This class needs existing instances of type xy to be created", which is what you are asking for. However, as you will set it as the Views DataContext in the constructor and as the DataContext can change after creation of the View, you cannot be sure that the View won't get a new ViewModel assigned after creation. Even worse, you will not be able to use your control in XAML anymore, because it doesn't have a default constructor anymore.

根据您的第一个问题,目前尚不清楚为什么 ViewModel 应该在 View 之前存在.如果您需要从 View 读取资源值并将其分配给 ViewModel 上的属性,我希望它是相反的吗?或者您是否正在访问 ViewModel 中的视图(不要!)?

According to your first question, it is not really clear why the ViewModel should exist prior to the View. If you need to read a resource value from your View and assign it to a property on your ViewModel, I would expect it to be the other way around? Or are you accessing the View in your ViewModel (don't!)?

问题是,你首先为什么要问这个问题.您(或您的老板...)的概念有一些非常错误的地方:View 和 ViewModel 是两个实体,它们应该在彼此不了解的情况下真正工作.我们的想法是构建可以在没有单个 View 的情况下完美运行的应用程序,只需在 ViewModels 上获取/设置值,并拥有可以在没有 ViewModels 的情况下完美编译任何运行的视图,只是没有任何显示或做的事情......要破解这种方法,最好根本不使用 MVVM.

The question is, why you have to ask this question in the first place. There is something pretty wrong in your (or your bosses...) concept: View and ViewModel are two entities which should really work without knowing about each other. The idea is to build applications that could work perfectly without a single View existing by just getting/setting values on ViewModels and to have Views which would compile any run perfectly well without ViewModels, just without anything to show or do... If you try to hack this approach, you're better off not using MVVM at all.

这篇关于执行第一个代码隐藏或查看模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆