c# - Add a New ViewModel to ViewModelLocator in MVVM Light Toolkit -


i know question basic, @ current moment, lost how should add new viewmodel viewmodellocator class in mvvm light toolkit.

my current implementation looks so:

first assume have window named settings, viewmodel named settingsviewmodel , viewmodellocator viewmodellocator.

first call createsettings() in viemodellocator constructor:

public viewmodellocator() {     if (viewmodelbase.isindesignmodestatic)     {      }     else     {         createsettings();     }      createmain(); } 

note run i'm not using blend , build application each time try run it. `createsettings() method.

i had no idea doing tried play safe , model after methods used creating , managing mainviewmodel.

public static void createsettings() {     if (_settings == null)     {         _settings = new settingsviewmodel();     } } 

then few methods modeled after used mainviewmodel:

[system.diagnostics.codeanalysis.suppressmessage("microsoft.performance",     "ca1822:markmembersasstatic",     justification = "this non-static member needed data binding purposes.")] public settingsviewmodel settings {         {         return settingsstatic;     } }  public static settingsviewmodel settingsstatic {         {         if (_settings == null)         {             createsettings();         }          return _settings;     } } 

and in settings window xaml:

<window x:class="_5500a_auto_calibrator.settings"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="settings" height="300" width="300"     datacontext="{binding source={staticresource locator}, path=settings}"> <window.resources>     <resourcedictionary>         <resourcedictionary.mergeddictionaries>             <resourcedictionary source="skins/mainskin.xaml" />         </resourcedictionary.mergeddictionaries>     </resourcedictionary> </window.resources> 

the window opened mainviewmodel so:

settings settings = new settings(); settings.show(); 

if try this, receive exception:

"'provide value on 'system.windows.staticresourceextension' threw exception.' line number '4' , line position '39'." 

and inner exception of:

"cannot find resource named 'locator'. resource names case sensitive." 

i've read on errors involving window's inability find locator resource, have blend.

my current take i'm doing wrong, there's little documentation adding new viewmodels i'm unsure i'm doing wrong.

edit:

my app.xaml:

<application x:class="_5500a_auto_calibrator.app"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:vm="clr-namespace:_5500a_auto_calibrator.viewmodel"              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"              startupuri="mainwindow.xaml"              mc:ignorable="d">      <application.resources>         <!--global view model locator-->         <vm:viewmodellocator x:key="locator"                              d:isdatasource="true" />     </application.resources>  </application> 

this how looks:

    public class viewmodellocator {     static viewmodellocator()     {         servicelocator.setlocatorprovider(() => simpleioc.default);          if (viewmodelbase.isindesignmodestatic)         {             simpleioc.default.register<idataservice, design.designdataservice>();         }         else         {             simpleioc.default.register<idataservice, dataservice>();             }          simpleioc.default.register<mainviewmodel>();     }      /// <summary>     /// gets main property.     /// </summary>     [system.diagnostics.codeanalysis.suppressmessage("microsoft.performance",         "ca1822:markmembersasstatic",         justification = "this non-static member needed data binding purposes.")]     public mainviewmodel main     {                 {             return servicelocator.current.getinstance<mainviewmodel>();         }     }      ... } 

the viewmodellocator static, yours doesn't seem be. sits in viewmodel folder (assuming installed mvvmlight nuget , added new wvvm project.

it proceeds have 2 cases design , runtime. (if don't use it, can skip if (isindesignmode) ... bit, , put logic. (though it's shame, since it's nice have preview of fake data in vs designer ...)

adding new viewmodels involves creating property of type, , registering them locator, can retrieve them ... differs , can done differently believe ...

hope help, , if there's else can with, let me know.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -