
{"id":666,"date":"2010-11-06T22:47:40","date_gmt":"2010-11-07T02:47:40","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=666"},"modified":"2010-11-06T22:47:40","modified_gmt":"2010-11-07T02:47:40","slug":"how-to-create-a-new-view-in-mvvm","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=666","title":{"rendered":"How to Create a New View in MVVM?"},"content":{"rendered":"<p>Let&#8217;s say I have a view that must open another window on command. Where would this code sit?<\/p>\n<p>The model? Don&#8217;t be ridiculous.<\/p>\n<p>The view model? Yes, it&#8217;s where the command handlers are, but view model is not supposed to know about the views, and this would not be testable.<\/p>\n<p>The View? Well, maybe, but aren&#8217;t the command handlers in view model? Besides, the view may not have enough data to create the desired window.<\/p>\n<p>I realized, that all WPF apps that I created so far had pretty much static UI defined at startup, and any simple dialogs that needed to be shown dynamically were created by a &#8220;dialog service&#8221;.<\/p>\n<p>Surprisingly, quick googling gives very little insight on the topic, although it comes with with <a href=\"http:\/\/stackoverflow.com\/questions\/303006\/m-v-vm-design-question-calling-view-from-viewmodel\">this answer<\/a> and <a href=\"http:\/\/johnpapa.net\/silverlight\/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum\/\">this article<\/a>.<\/p>\n<p>They seem to advise to create the views in a special layer (&#8220;controller&#8221;), talking to it via a service locator or an event broker. This kind of makes sense, because none of the 3 components of MVVM is well equipped for making new views. On the other hand, it is annoying, since it means one more moving part.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say I have a view that must open another window on command. Where would this code sit? The model? Don&#8217;t be ridiculous. The view model? Yes, it&#8217;s where the <a href=\"https:\/\/ikriv.com\/blog\/?p=666\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[12],"tags":[],"class_list":["entry","author-ikriv","post-666","post","type-post","status-publish","format-standard","category-wpf"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=666"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}