How to Initialize the view-model with a querystring parameter
Currently, I'm not using any sort of MVVM framework. Therefore, it is simple to fetch the query-string paremeter value on a navigated page. My scenario is a common one, I'm passing the primary key of an entity from the "Master Page" to the "Detail Page".
Right now, I'm trying to weigh the benefits of using an MVVM approach and this is a good example of the simple operations that can become conv开发者_StackOverflow社区oluted in a framework like MVVM. It could be that I've been looking in the wrong places, but it is a little frustrating not being able to readily find examples of this online.
Thanks!
You are not breaking the MVVM pattern by inserting some code in the view to pass the querystring to the ViewModel (perhaps use a factory pattern).
this.DataContext = ViewModelFactory.Create(HtmlPage.QueryString["MyVar"]);
You should use these patterns to assist you, and use them with common sense, sometimes they will overcomplicate things and backfire on extensibility/maintainability.
精彩评论