Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it bad to use ViewModelLocator to Grab other VM's for use in another Vm?
    primarykey
    data
    text
    <p>I am using MVVM light and figured out since that the ViewModelLocator can be used to grab any view model and thus I can use it to grab values.</p> <p>I been doing something like this</p> <pre><code>public class ViewModel1 { public ViewModel1() { var vm2 = new ViewModelLocator().ViewModel2; string name = vm2.Name; } } </code></pre> <p>This way if I need to go between views I can easily get other values. I am not sure if this would be best practice though(it seems so convenient makes me wonder if it is bad practice lol) as I know there is some messenger class thing and not sue if that is the way I should be doing it.</p> <p>Edit</p> <pre><code> static ViewModelLocator() { ServiceLocator.SetLocatorProvider(() =&gt; SimpleIoc.Default); SimpleIoc.Default.Register&lt;ViewModel1&gt;(); SimpleIoc.Default.Register&lt;ViewModel2&gt;(); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")] public ViewModel1 ViewModel1 { get { return ServiceLocator.Current.GetInstance&lt;ViewModel1 &gt;(); } } </code></pre> <p>Edit</p> <p>Here is a scenario that I am trying to solve.</p> <p>I have a view that you add price and store name to. When you click on the textbox for store name you are transferred to another view. This view has a textbox that you type the store you are looking for, as you type a select list get populated with all the possible matches and information about that store.</p> <p>The user then chooses the store they want. They are transferred back to the view where they "add the price", now the store name is filled in also.</p> <p>If they hit "add" button it takes the price, the store name, and the barcode(this came from the view BEFORE "add price view") and sends to a server.</p> <p>So as you can see I need data from different views. </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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