Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of implementing the first constructor, I suggest you implement a ViewModelLocator, like this:</p> <pre><code>public class ViewModelLocator { IoCContainer Container { get; set; } public IUserViewModel UserViewModel { get { return IoCContainer.Resolve&lt;IUserViewModel&gt;(); } } } </code></pre> <p>Then in XAML you declare the locator as a static resource:</p> <pre><code>&lt;local:ViewModelLocator x:Key="ViewModelLocator"/&gt; </code></pre> <p>While you initialize your application, it is necessary to provide the locator with the instance of the container:</p> <pre><code>var viewModelLocator = Application.Current.Resources["ViewModelLocator"] as ViewModelLocator; if(viewModelLocator == null) { // throw exception here } viewModelLocator.Container = IoCContainer; </code></pre> <p>Then in XAML you can use the resource cleanly:</p> <pre><code>&lt;UserControl DataContext="{Binding Path=UserViewModel, Source={StaticResource ViewModelLocator}}" /&gt; &lt;!-- The other user control properties --&gt; </code></pre> <p>For design time, you can implement a MockViewModelLocator:</p> <pre><code>public class MockViewModelLocator { public IUserViewModel UserViewModel { get { return new MockUserViewModel(); } } } </code></pre> <p>Declare it in XAML appropriately:</p> <pre><code>&lt;local:MockViewModelLocator x:Key="MockViewModelLocator"/&gt; </code></pre> <p>And finally use it in your user control:</p> <pre><code>&lt;UserControl d:DataContext="{Binding Path=UserViewModel, Source={StaticResource MockViewModelLocator}}" DataContext="{Binding Path=UserViewModel, Source={StaticResource ViewModelLocator}}" /&gt; &lt;!-- The other user control properties --&gt; </code></pre> <p>You can make your mock view model locator return safe and easily readable data for Blend to use and during runtime you will be using your real service.</p> <p>This way you do not lose design time data and you do not have to sacrifice the cleanliness of the dependency injection methodology in your view models.</p> <p>I hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload