c# - Which executes first Code Behind or View Model -
based on previous question accessing variables xaml , object viewmodel using code behind: how know executes first?
is code behind or viewmodel?
want make sure code behind executes prior viewmodel
the view , viewmodel both regular classes instantiated. done calling constructor in other class. so, simple answer question: set breakpoint in each constructor , see 1 gets hit first.
there no general answer question because depends on architecture , use case. often, control bound property of viewmodel of it's parent, changes @ point. @ point view exists , have no idea how long value property has been set existing already. in other cases, view created specific viewmodel , takes constructor parameter.
the 1 way make sure viewmodel exists before view pass viewmodel constructor parameter. idea behind constructor parameters express: "this class needs existing instances of type xy created", asking for. however, set views datacontext
in constructor , datacontext
can change after creation of view, cannot sure view won't new viewmodel assigned after creation. worse, not able use control in xaml anymore, because doesn't have default constructor anymore.
according first question, not clear why viewmodel should exist prior view. if need read resource value view , assign property on viewmodel, expect other way around? or accessing view in viewmodel (don't!)?
the question is, why have ask question in first place. there pretty wrong in (or bosses...) concept: view , viewmodel 2 entities should work without knowing each other. idea build applications work without single view existing getting/setting values on viewmodels , have views compile run without viewmodels, without show or do... if try hack approach, you're better off not using mvvm @ all.
Comments
Post a Comment