Skip to content

Instantly share code, notes, and snippets.

@treytomes
Created February 2, 2016 15:18
Show Gist options
  • Save treytomes/cf5d02885486a8df42d5 to your computer and use it in GitHub Desktop.
Save treytomes/cf5d02885486a8df42d5 to your computer and use it in GitHub Desktop.
MvvmLight for VB.NET
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MyMvvmApplication.ViewModel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d"
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
StartupUri="MainWindow.xaml">
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</Application.Resources>
</Application>
Imports GalaSoft.MvvmLight
Namespace ViewModel
''' <summary>
''' This class contains properties that the main View can data bind to.
''' <para>
''' Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
''' </para>
''' <para>
''' You can also use Blend to data bind with the tool's support.
''' </para>
''' <para>
''' See http://www.galasoft.ch/mvvm
''' </para>
''' </summary>
Public Class MainViewModel
Inherits ViewModelBase
''' <summary>
''' Initializes a new instance of the MainViewModel class.
''' </summary>
Public Sub New()
'if IsInDesignMode then
' ' Code runs in Blend --> create design time data.
'else
' ' Code runs "for real"
'end if
End Sub
End Class
End Namespace
' In App.xaml:
' <Application.Resources>
' <vm:ViewModelLocator xmlns:vm="clr-namespace:WirePicking"
' x:Key="Locator" />
' </Application.Resources>
'
' In the View:
' DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
'
' You can also use Blend to do all this with the tool's support.
' See http://www.galasoft.ch/mvvm
Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.Ioc
Imports Microsoft.Practices.ServiceLocation
Namespace ViewModel
''' <summary>
''' This class contains static references to all the view models in the
''' application and provides an entry point for the bindings.
''' </summary>
Public Class ViewModelLocator
''' <summary>
''' Initializes a new instance of the ViewModelLocator class.
''' </summary>
Public Sub New()
ServiceLocator.SetLocatorProvider(Function() SimpleIoc.Default)
'if ViewModelBase.IsInDesignModeStatic then
' ' Create design time view services and models
' SimpleIoc.Default.Register(Of IDataService, DesignDataService)();
'else
' ' Create run time view services and models
' SimpleIoc.Default.Register(Of IDataService, DataService)();
'end if
SimpleIoc.Default.Register(Of MainViewModel)()
End Sub
Public ReadOnly Property Main As MainViewModel
Get
Return ServiceLocator.Current.GetInstance(Of MainViewModel)()
End Get
End Property
Public Shared Sub Cleanup()
' TODO: Clear the ViewModels
End Sub
End Class
End Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment